From: 7stud -- Newsgroups: comp.lang.ruby Subject: Re: Understanding global variables. Date: Thu, 14 Apr 2011 20:05:03 -0500 Organization: Service de news de lacave.net Lines: 66 Message-ID: <0344bd66a55be0b7a3d9ceb2739042e6@ruby-forum.com> References: <962f80bb11f2292d2ba5498cbfca785c@ruby-forum.com> NNTP-Posting-Host: bristol.highgroove.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Trace: talisker.lacave.net 1302830283 19241 65.111.164.187 (15 Apr 2011 01:18:03 GMT) X-Complaints-To: abuse@lacave.net NNTP-Posting-Date: Fri, 15 Apr 2011 01:18:03 +0000 (UTC) In-Reply-To: <962f80bb11f2292d2ba5498cbfca785c@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: 381595 X-Ml-Name: ruby-talk X-Rubymirror: Yes X-Ruby-Talk: <0344bd66a55be0b7a3d9ceb2739042e6@ruby-forum.com> Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.stben.net!talisker.lacave.net!lacave.net!not-for-mail Xref: x330-a1.tempe.blueboxinc.net comp.lang.ruby:2905 Fily Salas wrote in post #992901: > Hi, > > I thought that I understood the different type of variables in Ruby but > now that I'm actually trying them I see that I didn't, reading is easy > putting things in practice is hard. > > class Car > @mil_gal = 30 > @fuel_gal = 10 > > def go(mil_gal, fuel_gal) > @mil_gal = mil_gal > @fuel_gal=fuel_gal > millage =@mil_gal * @fuel_gal > puts millage > end > end > > car = Car.new > car.go(10,50) > > In the above code I have two global variables @mil_gal = 30 and > @fuel_gal = 10, Wrong. In ruby, global variables start with a $ sign and they are visible everywhere(that is except for the regex 'global' variables that ruby predefines) > well let me rephrase this in my eyes they are global > variables... You may think those variables are global to the class, i.e. they can be referred to in any method definition--but they cannot. "Instance variables", the ones that start with '@' attach themselves to whatever object is self at the time they are created, and inside a class definition but outside of any method definitions self is equal to the class: class A puts self end --output:-- A In your case, the instance variables attach to the class, and therefore you must use the class as the receiver: Car.mil_gal Those are known as "class instance variables" and are preferred over class variables, e.g. @@mil_gal. A variable that is visible in all method definitions within a class is called an "instance variable", and it begins with "@" and can be created inside a method definition to get the behaviour you want. Inside a method definition, self is equal to the object that called the method, so the instance variable attaches to the object. In ruby, classes carry the common methods of all instances of the class, and instances each have their own instance variables. -- Posted via http://www.ruby-forum.com/.