method self.a= and a= create different instance variables

Y

yuesefa

i try this in irb .

irb(main):001:0> def a=(a)
irb(main):002:1> @a=a
irb(main):003:1> end
=> nil
irb(main):004:0> def a
irb(main):005:1> @a
irb(main):006:1> end
=> nil
irb(main):007:0> a=1
=> 1
irb(main):008:0> a
=> 1
irb(main):009:0> self.a=2
=> 2
irb(main):010:0> a
=> 1
irb(main):011:0> self.a
=> 2

a and a= should be private methods of Object. so call of a or self.a
should do the same thing. but it's not. i am now confused.
 
D

dblack

Hi --

i try this in irb .

irb(main):001:0> def a=(a)
irb(main):002:1> @a=a
irb(main):003:1> end
=> nil
irb(main):004:0> def a
irb(main):005:1> @a
irb(main):006:1> end
=> nil
irb(main):007:0> a=1
=> 1
irb(main):008:0> a
=> 1
irb(main):009:0> self.a=2
=> 2
irb(main):010:0> a
=> 1
irb(main):011:0> self.a
=> 2

a and a= should be private methods of Object. so call of a or self.a
should do the same thing. but it's not. i am now confused.

When you write:

a = 1

Ruby parses that as a local variable assignment. In other words,
methods ending in = always require that you provide an explicit
receiver (in this case "self").


David

--
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
http://www.manning.com/black => RUBY FOR RAILS (reviewed on
Slashdot, 7/12/2006!)
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
(e-mail address removed) => me
 
T

Tom Rauchenwald

yuesefa said:
i try this in irb .

irb(main):001:0> def a=(a)
irb(main):002:1> @a=a
irb(main):003:1> end
=> nil
irb(main):004:0> def a
irb(main):005:1> @a
irb(main):006:1> end
=> nil
irb(main):007:0> a=1
=> 1
irb(main):008:0> a
=> 1

irb(main):010:0> @a
=> nil
irb(main):011:0> self.a
=> nil
That means that not the method a= was called, but the local variable a was set
to one.
irb(main):009:0> self.a=2
=> 2

Here you expicitly call the method, so @a is set to 2.
irb(main):010:0> a
=> 1

But the local variable a is still 1.
irb(main):011:0> self.a
=> 2

Here the method a is explicitly called, so you get what you expected.
a and a= should be private methods of Object. so call of a or self.a
should do the same thing. but it's not. i am now confused.

It's simply because the ruby-interpreter can't tell what you want when you
write a=1 -- do you want to set a to 1, or do you want to call the method a?

HTH,
Tom
 
L

Leslie Viljoen

Hi --



When you write:

a = 1

Ruby parses that as a local variable assignment. In other words,
methods ending in = always require that you provide an explicit
receiver (in this case "self").

also,

@a

will give you the same as

self.a

which makes sense because you are "inside" the definition of Object:

irb(main):085:0> self
=> #<Object:0x277fa2c @a=7>

Les
 

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

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,141
Latest member
BlissKeto
Top