Question about attr_accessor

  • Thread starter Roland Swingler
  • Start date
R

Roland Swingler

Hi,

I came across something unexpected today, and wondered if anyone could
explain it to me. If I have this class:

class Foo
def initialize
@bar = 0
end

attr_accessor :bar

def add
bar += 1
end
end

then the "add" function doesn't work - it complains about a nil
object. You have to specifically put "self.bar += 1". Why is that - I
thought that the "self" was implicitly there?

Doing:

f = Foo.new
f.add

works as the class stands, so you only seem to need the self if you
are calling the accessor from within your class.

Confused...

TIA
Roland
 
R

Robert Klemme

Hi,

I came across something unexpected today, and wondered if anyone could
explain it to me. If I have this class:

class Foo
def initialize
@bar = 0
end

attr_accessor :bar

def add
bar += 1
end
end

then the "add" function doesn't work - it complains about a nil
object. You have to specifically put "self.bar += 1". Why is that - I
thought that the "self" was implicitly there?

bar += 1 is translated to "bar = bar + 1". When Ruby sees "bar =" it
takes bar to be a local variable. Hence you have to prefix with
"self.". See:
http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html#UO

Kind regards

robert
 
H

Harry

Hi,

I came across something unexpected today, and wondered if anyone could
explain it to me. If I have this class:

class Foo
def initialize
@bar = 0
end

attr_accessor :bar

def add
bar += 1
end
end

then the "add" function doesn't work - it complains about a nil
object. You have to specifically put "self.bar += 1". Why is that - I
thought that the "self" was implicitly there?

Doing:

f = Foo.new
f.add

works as the class stands, so you only seem to need the self if you
are calling the accessor from within your class.

Confused...

TIA
Roland
attr_accessor :bar refers to the instance variable @bar.
bar and @bar are not the same.


Harry
 
D

dblack

Hi --

Hi,

I came across something unexpected today, and wondered if anyone could
explain it to me. If I have this class:

class Foo
def initialize
@bar = 0
end

attr_accessor :bar

def add
bar += 1
end
end

then the "add" function doesn't work - it complains about a nil
object. You have to specifically put "self.bar += 1". Why is that - I
thought that the "self" was implicitly there?

For methods that end in = (like bar=), you always need an explicit
receiver, even if it's self, because otherwise Ruby will see it as an
assignment to a local variable.


David

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top