나는 지금까지 해왔 던 프로그래밍 문제를 찾아내는 데 도움이 필요합니다.Ruby에서 HTML 구문 분석기 만들기
문제 설명 :
HTML 문서 (문자열)와 키워드 (또한 문자열)을 받아 루비에서 함수를 작성합니다. 이 함수는 키워드가 HTML 태그 안에 나타나지 않는 한 <body>
요소 뒤에 HTML 문자열에있는 키워드가 모두 발견 된 다음 태그에서 발견 된 문자열을 둘러싸고 키워드를 강조 표시합니다. 예를 들어,
<span style="background-color: blue; color: white">keyword</span>
당신은 HTML 태그 내에서 발생하는 문자열을 강조하지 않도록주의해야합니다. 키워드는 예를 들어,``테이블 '', 당신은 이 마크 업 싶지 않을 것이다 :
<table width="100%" border="0">
지금까지했던 어떤 : 오류
puts "Welcome to the HTML keyword highlighter!"
puts "Please Enter A Keyword: "
keyword = gets.chomp
canEdit = false
infile = File.new("desktop/code.html", "r")
outfile = File.new("Result.html", "w")
infile.each{ |i|
if (i.include? "<body>")
canEdit = true
end
if (i.include? "</body>")
canEdit = false
end
if(canEdit == true)
keyword.gsub(keyword, "<span style=\"background-color: yellow; color: black\">#{keyword}</span>")
outfile.write i
end
outfile.close()
infile.close()
}
현재 수신 :
HTML 키워드 하이 라이터에 오신 것을 환영합니다. 나는이 오류의 원인과 문제를 해결하기 위해 몇 가지 지침을 사용할 수 있는지 확실 해요
/Users/Eva/Desktop/Personal/part4_program.rb:16:in `each': closed stream (IOError)
from /Users/Eva/Desktop/Personal/part4_program.rb:16:in `<main>'
간단한 :
키워드를 입력하시기 바랍니다. 또한이 프로그램이 프로그래밍 문제에 대한 해답으로 올바른 방향으로 가고 있는지 궁금합니다. 나는 Nokogiri가 이미 자원으로 이용 가능하다는 것을 알고 있지만 더 나은 선택이 될 수 없다면 Nokogiri를 사용하지 않아야한다고 생각했습니다.
이puts 'Welcome to the HTML keyword highlighter!'
puts 'Please Enter A Keyword: '
keyword = gets.chomp
can_edit = false
infile = File.new('desktop/code.html', 'r')
outfile = File.new('Result.html', 'w')
infile.each {|i|
if i.include?('<body>')
can_edit = true
end
if i.include?('</body>')
can_edit = false
end
if can_edit
keyword.gsub(keyword, %Q[<span style="background-color: yellow; color: black">#{keyword}</span>])
outfile.write i
end
outfile.close
infile.close
}
오류 메시지가 말한다 : 그래서
part4_program.rb:16:in `each': closed stream (IOError)
, 무슨 일이 일어나고
더 나은 옵션입니다. http://stackoverflow.com/a/1732454/438992 –
왜 더 나아 졌습니까? 나는 그것을 성공시키지 않고 설치하려고 시도했다. 내가 설치하려고 할 때마다이 오류가 발생합니다 : 오류 : 보석을 실행하는 중 ...(Gem :: FilePermissionError) /Library/Ruby/Gems/2.0.0 디렉토리에 대한 쓰기 권한이 없습니다. – mm19
YMMV하지만 설치에 도움이 될 수 있습니다. http://stackoverflow.com/questions/14607193/installing-gem-or-updating-rubygems-fails-with-permissions-error – orde