Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!news.stack.nl!talisker.lacave.net!lacave.net!not-for-mail From: 7stud -- Newsgroups: comp.lang.ruby Subject: Re: Understanding global variables. Date: Fri, 15 Apr 2011 13:10:53 -0500 Organization: Service de news de lacave.net Lines: 66 Message-ID: References: <962f80bb11f2292d2ba5498cbfca785c@ruby-forum.com> <2d350d202910142ee0f58ef60b4317c9@ruby-forum.com> NNTP-Posting-Host: bristol.highgroove.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: Quoted-printable X-Trace: talisker.lacave.net 1302891700 33025 65.111.164.187 (15 Apr 2011 18:21:40 GMT) X-Complaints-To: abuse@lacave.net NNTP-Posting-Date: Fri, 15 Apr 2011 18:21:40 +0000 (UTC) In-Reply-To: <2d350d202910142ee0f58ef60b4317c9@ruby-forum.com> X-Received-From: This message has been automatically forwarded from the ruby-talk mailing list by a gateway at comp.lang.ruby. If it is SPAM, it did not originate at comp.lang.ruby. Please report the original sender, and not us. Thanks! For more details about this gateway, please visit: http://blog.grayproductions.net/categories/the_gateway X-Mail-Count: 381656 X-Ml-Name: ruby-talk X-Rubymirror: Yes X-Ruby-Talk: Xref: x330-a1.tempe.blueboxinc.net comp.lang.ruby:2963 Fily Salas wrote in post #992993: > > This may be confusing because I have never heard about =E2=80=9CSELF=E2= =80=9D statement, > =E2=80=9CINITIALZE=E2=80=9D method and the =E2=80=9C:=E2=80=9D notation= , I guess I need to read more > about the language. > Knowing which object is 'self' is sort of an intermediate topic, but it = is critical to understanding how ruby works. > What would happen if I position a variable using the wrong syntax? In > other words if I position a variable where an instance variable would > normally go but without the @ will Ruby get confused and treat this > differently and may get an error or the interpreter will simply use it > and keep track of what kind of variable it is by itself. > A variable name that is not preceded by an '@', is called a 'local = variable', and a local variable ceases to exist once the method ends. = Here is an example: class Dog def initialize(a_name, a_color) @name =3D a_name color =3D a_color end def name @name #same as 'return @name' end def name=3D(str) @name =3D str end def color @color #same as 'return @color' end end my_dog =3D Dog.new('Spot', 'black') #Calling new() automatically causes initialize() #to execute. puts my_dog.name #=3D> Spot my_dog.name =3D 'Max' puts my_dog.name #=3D> Max puts my_dog.color #=3D> > Any good tutorial about variables in Ruby? "Beginning Ruby (2nd ed)" by Cooper -- = Posted via http://www.ruby-forum.com/.=