【发布时间】:2012-09-11 02:22:08
【问题描述】:
我有一个通过获取 html 并使用 split 函数创建的字符串数组。我正在尝试遍历它:
html_array.each do |line|
...
end
我可以puts 使用puts line.index('text') 在每个line 中的字符串的索引,并且我可以puts 使用puts line[17] 的索引处的字符(两者都不打印nil),但是当我结合像
puts line[line.index('text')]
它给了我错误:
no implicit conversion from nil to integer (TypeError)
谁能告诉我我做错了什么?顺便说一句,我是编程和 ruby 新手。
【问题讨论】:
-
使用
p,而不是puts:html_array.each { |line| p line.index 'text' }你可能有不匹配的行。您也不应该使用split函数解析 HTML。请改用Nokogiri。