[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Question about attr_accessor

knaveofdiamonds

3/3/2007 12:55:00 PM

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

5 Answers

Martin Boese

3/3/2007 1:09:00 PM

0

Change your add method to:

> def add
> @bar+=1
> end

If you use just 'bar' it will think of a local variable which is not defined.

martin


On Saturday 03 March 2007 13:00, Roland Swingler wrote:
> 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

Robert Klemme

3/3/2007 1:10:00 PM

0

On 03.03.2007 13:55, Roland Swingler wrote:
> 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/langua...

Kind regards

robert

Harry

3/3/2007 1:12:00 PM

0

On 3/3/07, Roland Swingler <roland.swingler@gmail.com> wrote:
> 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

--
http://www.kakueki.com/ruby...
Japanese Ruby List Subjects in English

dblack

3/3/2007 1:14:00 PM

0

knaveofdiamonds

3/3/2007 2:01:00 PM

0

Great - thanks for clearing that up.