Pergunta

I extended the Nokogiri::HTML:Document class as follows to add a function called 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

I placed this code in a file located at lib/nokogiri/html/document.rb in my rails 3 application tree

I call this function as follows

if @html_doc.on_same_line?(node,node2,font_size_hash)
   ### do something
end

This code is in a separate custom ruby class that I created. This class is MyClass and is in MyModule. The code for this class is placed under lib/my_module/my_class.rb

Now when this code is executed as part of my rails application, I get the following error.

unexpected '?' after '[#<Nokogiri::CSS::Node:0x00000100dfebd8 @type=:ELEMENT_NAME, @value=["on_same_line"]>]'

I have no idea why this is happening. Could someone please help me

Also when I run this same code outside the rails app, I dont get any errors, and it works as expected

Thanks Paul

Foi útil?

Solução

In rails 3 you need to put your monkey patch code under config/initializers instead of under lib

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top