已经初始化的常量警告
我使用 Nokogiri 代码提取 HTML 节点之间的文本,并在读取文件列表时收到这些错误。我使用简单的嵌入式 HTML 时没有遇到错误。我想消除或抑制这些警告,但不知道如何做。警告出现在每个块的末尾:
extract.rb:18: warning: already initialized constant EXTRACT_RANGES
extract.rb:25: warning: already initialized constant DELIMITER_TAGS
这是我的代码:
#!/usr/bin/env ruby -wKU
require 'rubygems'
require 'nokogiri'
require 'fileutils'
source = File.open('/documents.txt')
source.readlines.each do |line|
line.strip!
if File.exists? line
file = File.open(line)
doc = Nokogiri::HTML(File.read(line))
# suggested by dan healy, stackoverflow
# Specify the range between delimiter tags that you want to extract
# triple dot is used to exclude the end point
# 1...2 means 1 and not 2
EXTRACT_RANGES = [
1...2
]
# Tags which count as delimiters, not to be extracted
DELIMITER_TAGS = [
"h1",
"h2",
"h3"
]
extracted_text = []
i = 0
# Change /"html"/"body" to the correct path of the tag which contains this list
(doc/"html"/"body").children.each do |el|
if (DELIMITER_TAGS.include? el.name)
i += 1
else
extract = false
EXTRACT_RANGES.each do |cur_range|
if (cur_range.include? i)
extract = true
break
end
end
if extract
s = el.inner_text.strip
unless s.empty?
extracted_text << el.inner_text.strip
end
end
end
end
print("\n")
puts line
print(",\n")
# Print out extracted text (each element's inner text is separated by newlines)
puts extracted_text.join("\n\n")
end
end
I'm using Nokogiri code to extract text between HTML nodes, and getting these errors when I read in a list of files. I didn't get the errors using simple embedded HTML. I'd like to eliminate or suppress the warnings but don't know how. The warnings come at the end of each block:
extract.rb:18: warning: already initialized constant EXTRACT_RANGES
extract.rb:25: warning: already initialized constant DELIMITER_TAGS
Here is my code:
#!/usr/bin/env ruby -wKU
require 'rubygems'
require 'nokogiri'
require 'fileutils'
source = File.open('/documents.txt')
source.readlines.each do |line|
line.strip!
if File.exists? line
file = File.open(line)
doc = Nokogiri::HTML(File.read(line))
# suggested by dan healy, stackoverflow
# Specify the range between delimiter tags that you want to extract
# triple dot is used to exclude the end point
# 1...2 means 1 and not 2
EXTRACT_RANGES = [
1...2
]
# Tags which count as delimiters, not to be extracted
DELIMITER_TAGS = [
"h1",
"h2",
"h3"
]
extracted_text = []
i = 0
# Change /"html"/"body" to the correct path of the tag which contains this list
(doc/"html"/"body").children.each do |el|
if (DELIMITER_TAGS.include? el.name)
i += 1
else
extract = false
EXTRACT_RANGES.each do |cur_range|
if (cur_range.include? i)
extract = true
break
end
end
if extract
s = el.inner_text.strip
unless s.empty?
extracted_text << el.inner_text.strip
end
end
end
end
print("\n")
puts line
print(",\n")
# Print out extracted text (each element's inner text is separated by newlines)
puts extracted_text.join("\n\n")
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果代码缩进正确,就会更容易注意到常量定义是在循环内完成的。
比较
与
If the code were properly indented, it'd be easier to notice that the constant definition was being done within a loop.
Compare
with
之前没注意到。只需将常量移出每个块即可
Didn't notice earlier. Just move the constants out of the each block
作为编程技巧:
在范围定义中使用
...
与..
时要小心。三点版本不像两点版本那么常用,而且额外的点很容易被忽略,从而使代码更难维护。我必须有一个非常好的理由来使用三点。比较 IRB 的这些输出:与,
看看第一个有多么误导。
As a programming tip:
Be careful using
...
vs...
for range definitions. The three-dot version isn't as commonly used as the two-dot version, and that extra dot can be easy to miss, making the code harder to maintain. I'd have to have a VERY good reason to use three-dots. Compare these outputs from IRB:vs.
to see how misleading the first is.