Rafa_F
August 5, 2013, 10:35pm
1
All I want to do, is to create a new variable called data_dfp inside the
class, and be able to set/get it’s value from outside
require ‘nokogiri’
class MyDocument < Nokogiri::XML::SAX::Document
attr_accessor :data_dfp
def start_element(name, attrs = [])
puts “#{name}”
end
end
parser = Nokogiri::XML::SAX::Parser.new(MyDocument.new)
parser.data_dfp = 4
puts parser.data_dfp
Error
nokoteste.rb:12:in <main>': undefined method
data_dfp=’ for
#Nokogiri::XML::SAX::Parser:0x007fcc1413bea0 (NoMethodError)
Whats the right way of doing this?
On 06/08/13 08:35, Renato Co wrote:
end
parser = Nokogiri::XML::SAX::Parser.new(MyDocument.new)
^^ I’m not sure what this is supposed to mean.
Have you tried simply:
parser = MyDocument.new
I’m not familiar with nokogiri, is it supposed to take something in its
constructor?
parser.data_dfp = 4
puts parser.data_dfp
Error
nokoteste.rb:12:in <main>': undefined method
data_dfp=’ for
#Nokogiri::XML::SAX::Parser:0x007fcc1413bea0 (NoMethodError)
Whats the right way of doing this?
Sam
Sam D. wrote in post #1117822:
On 06/08/13 08:35, Renato Co wrote:
end
parser = Nokogiri::XML::SAX::Parser.new(MyDocument.new)
^^ I’m not sure what this is supposed to mean.
I with I knew it too…
Several attempts later turns out this works(for now)
parser = Nokogiri::XML::SAX::Parser.new(MyDocument.new(4))
I am very novice at ruby in general…
ty for helping
forget to say I added a method
def initialize(data=[])
@data_dfp = data
end
On 06/08/13 10:19, Renato Co wrote:
parser = Nokogiri::XML::SAX::Parser.new(MyDocument.new(4))
I am very novice at ruby in general…
ty for helping
Cool, go forth and conquer =]
On Mon, Aug 5, 2013 at 5:35 PM, Renato Co [email protected] wrote:
end
parser = Nokogiri::XML::SAX::Parser.new(MyDocument.new)
parser.data_dfp = 4
puts parser.data_dfp
data_fp is defined at the MyDocument and not at the parser.
parser.document # Returns the “MyDocument” instance you want.
So you should…
parser.document.data_dfp = 4
puts parser.document.data_dfp
Was this what you were trying to accomplish?
On Aug 5, 2013, at 3:35 PM, Renato Co [email protected] wrote:
All I want to do, is to create a new variable called data_dfp inside the
class, and be able to set/get it’s value from outside
require ‘nokogiri’
class MyDocument < Nokogiri::XML::SAX::Document
When you did this, you created a new class that inherits from
Nokogiri::XML::SAX::Document
attr_accessor :data_dfp
def start_element(name, attrs = [])
puts “#{name}”
end
end
parser = Nokogiri::XML::SAX::Parser.new(MyDocument.new)
But then you assigned parser to a new Nokogiri::XML::SAX::Parser object.
The MyDocument.new will get eaten up by the enclosing .new. Since you
defined the instance variable and setters/getters in the inherited
class, they will not be in the Nokogiri::XML::SAX::Parser class, and
thus not in the instance.
If instead you make parser an instance of MyDocument, you’d get them:
parser = MyDocument.new