Hello,
I did the following;
class O
@b = 0
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 #<O:0xb7aac424 @b=1> 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?
Best,
2008/9/1 Mayuresh K. [email protected]:
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.
from /usr/bin/irb1.9:13:in `<main>'
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
Robert K. wrote:
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.
Exactly. And if you want to initialize @b for each new instance of O to
the value of 0, you should write
class O
def initialize
@b = 0
end
def x=(a)
@b = a
end
attr_reader :b
end
Now all new instances will have @b equal to 0. And the attr_reader is
for reading the instance variables. In fact attr_reader :b is the same
as writing explicitly
def b
@b
end
TPR.
Additionally:
c = o.b
This always means: “run the method ‘b’ on object ‘o’” - not “give me
the instance variable called ‘b’ on object ‘o’”: instance variables are
never in scope outside of the class definition. (So they would be
‘private’ instance variables in Java for instance).
I just thought it was worth making that point - the stack trace you
provided shows that this is going on - its complaining about ‘no such
method’, rather than ‘no such instance variable’ or something like that.
This tallies up by the way with the fact that Ruby allows optional
brackets (“parentheses” depending on where you are from) for method
calls.
“c=o.b” is equivalent to “c=o.b()”.
So you get code that looks like its reading a instance variable, but
really its executing a method.
John
how do I access an instance variable if not via
object.instance_variable?
To access instance variable, you need to define attribute
reader/writer/accessor inside the class.
Attribute reader definition syntax
attr_reader :a, :b, :c
or
attr_reader ‘a’, ‘b’, ‘c’
Attribute writer definition syntax
attr_writer :a, :b, :c
or
attr_writer ‘a’, ‘b’, ‘c’
Attribute accessor (support read and write) definition syntax
attr_accessor :a, :b, :c
or
attr_accessor ‘a’, ‘b’, ‘c’
Example of Class Obj
class Obj
attr_reader :a
attr_writer :b
attr_accessor :c
def set_a(param)
@a = param
end
def get_b
@b
end
end
a_obj = Obj.new
a_obj.set_a = 1
puts a_obj.a
a_obj.b = 2
puts a_obj.get_b
a_obj.c = 3
puts a_obj.c