From: Robert Klemme Date: 2008-09-01T21:03:56+09:00 Subject: Re: accessing instance variable 2008/9/1 Mayuresh Kathe : > Hello, > > I did the following; > class O > @b = 0 You are not defining an instance variable of instances of O here but of O itself. In other words this @b and the @b below have nothing to do with each other. > def x=(a) > @b = a > end > end > > o = O.new > o.x = 1 > > I then tried to access b by doing; > c = o.b > > but irb gave me the following error; > NoMethodError: undefined method `b' for # > from (irb):10 > from /usr/lib/ruby/1.9.0/irb.rb:150:in `block (2 levels) in eval_input' > from /usr/lib/ruby/1.9.0/irb.rb:259:in `signal_status' > from /usr/lib/ruby/1.9.0/irb.rb:147:in `block in eval_input' > from /usr/lib/ruby/1.9.0/irb.rb:146:in `eval_input' > from /usr/lib/ruby/1.9.0/irb.rb:70:in `block in start' > from /usr/lib/ruby/1.9.0/irb.rb:69:in `catch' > from /usr/lib/ruby/1.9.0/irb.rb:69:in `start' > from /usr/bin/irb1.9:13:in `
' > > how do I access an instance variable if not via object.instance_variable? Please look up the docs for attr_accessor, attr, attr_reader and attr_writer. Kind regards robert -- use.inject do |as, often| as.you_can - without end