From: Robert Klemme Date: 2011-09-16T06:22:52+09:00 Subject: Re: Some newbie questions On Thu, Sep 15, 2011 at 10:57 PM, Vladimir Van Bauenhoffer 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 >>   def initialize(name) >>        @name=name >>   end >> >>   def say_your_name >>        puts @name >>   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 = {}, sorry for being unclear. myhash = {} is the same as myhash = Hash.new - only less typing. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/