From: Tamara Temple Date: 2013-08-05T21:10:02-05:00 Subject: Re: Creating variable inside a class On Aug 5, 2013, at 3:35 PM, Renato Co 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 > parser.data_dfp = 4 > puts parser.data_dfp > > Error > nokoteste.rb:12:in `
': undefined method `data_dfp=' for > # (NoMethodError) > > > Whats the right way of doing this? > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ ruby-talk mailing list ruby-talk@ruby-lang.org http://lists.ruby-lang.org/cgi-bin/mailman/listinfo/ruby-talk