Nokogiri :: HTML:Documentクラスを次のように拡張して、on_same_lineという関数を追加しましたか?
module Nokogiri
module HTML
class Document
def on_same_line?(node1,node2,font_sizes)
return false if node1.nil? or node2.nil? or font_sizes.nil?
return false if (node1.name != "div" || node2.name != "div")
fsize1 = font_sizes[node1.children[0].children[0].attributes["class"].value]
fsize2 = font_sizes[node2.children[0].children[0].attributes["class"].value]
style1 = node1.attributes["style"].value
style2 = node2.attributes["style"].value
top1 = style1.split(/;/)[1].split(/:/)[1].to_i
top2 = style2.split(/;/)[1].split(/:/)[1].to_i
(top1 + fsize1 - top2 - fsize2).abs < 4
end
end
end
end
このコードを、Rails3アプリケーションツリーのlib/ nokogiri / html/document.rbにあるファイルに配置しました。
この関数を次のように呼び出します
if @html_doc.on_same_line?(node,node2,font_size_hash)
### do something
end
このコードは、私が作成した別のカスタムrubyクラスにあります。このクラスはMyClassであり、MyModuleにあります。このクラスのコードは、lib / my_module/my_class.rbの下に配置されます。
このコードをRailsアプリケーションの一部として実行すると、次のエラーが発生します。
unexpected '?' after '[#<Nokogiri::CSS::Node:0x00000100dfebd8 @type=:ELEMENT_NAME, @value=["on_same_line"]>]'
なぜこれが起こっているのか分かりません。誰か助けてくれませんか
また、これと同じコードをRailsアプリの外部で実行しても、エラーは発生せず、期待どおりに機能します
ありがとうポール