On Thu, Sep 15, 2011 at 10:57 PM, Vladimir Van Bauenhoffer
<cluny_gisslaren / hotmail.com> wrote:
> alberto kaputchella:
>>
>> The @ means that the variable is an object variable, its scope is
>> within, and is associated to, the current object.
>>
>> For example,
>>
>> class Person
>> =A0 def initialize(name)
>> =A0 =A0 =A0 =A0@name=3Dname
>> =A0 end
>>
>> =A0 def say_your_name
>> =A0 =A0 =A0 =A0puts @name
>> =A0 end
>>
>> In this class, you assign the name provided to the class to @name.

Not to the class - to the _instance_!  Or more precisely the name is
passed to Person#new which forwards it to #initialize after allocating
the instance.

>> @name as an object variable, is then accessible from any other method
>> inside the object.
>
> So the @-sign in Ruby is like self in Python? Can someone confirm who
> knows Python?

I do not know Python but I assume "self" in Python is the same as
"self" in Ruby: a reference to the current object.

Variable names prefixed with @ really set the scope of the variable to
be the instance and not the current method.  "@foo" is an instance
variable of whatever self is at the moment and "foo" is a local
variable.  I suggest you play around with these a bit - I am sure you
will quickly notice the difference.

> Chad Perrin:
>>What exactly do you mean by "the normal way"?
>
> I meant like this: myhash =3D {}, sorry for being unclear.

myhash =3D {} is the same as myhash =3D Hash.new - only less typing.

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/