Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #2905
| From | 7stud -- <bbxx789_05ss@yahoo.com> |
|---|---|
| Newsgroups | comp.lang.ruby |
| Subject | Re: Understanding global variables. |
| Date | 2011-04-14 20:05 -0500 |
| Organization | Service de news de lacave.net |
| Message-ID | <0344bd66a55be0b7a3d9ceb2739042e6@ruby-forum.com> (permalink) |
| References | <962f80bb11f2292d2ba5498cbfca785c@ruby-forum.com> |
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/.
Back to comp.lang.ruby | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Understanding global variables. Fily Salas <fs_tigre@hotmail.com> - 2011-04-14 19:55 -0500
Re: Understanding global variables. 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-14 20:05 -0500
Re: Understanding global variables. jake kaiden <jakekaiden@yahoo.com> - 2011-04-14 22:30 -0500
Re: Understanding global variables. Fily Salas <fs_tigre@hotmail.com> - 2011-04-15 08:13 -0500
Re: Understanding global variables. 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-15 13:10 -0500
Re: Understanding global variables. jake kaiden <jakekaiden@yahoo.com> - 2011-04-15 08:36 -0500
Re: Understanding global variables. Fily Salas <fs_tigre@hotmail.com> - 2011-04-15 09:14 -0500
Re: Understanding global variables. jake kaiden <jakekaiden@yahoo.com> - 2011-04-15 09:20 -0500
Re: Understanding global variables. Fily Salas <fs_tigre@hotmail.com> - 2011-04-16 14:47 -0500
csiph-web