Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #2901

Understanding global variables.

From Fily Salas <fs_tigre@hotmail.com>
Newsgroups comp.lang.ruby
Subject Understanding global variables.
Date 2011-04-14 19:55 -0500
Organization Service de news de lacave.net
Message-ID <962f80bb11f2292d2ba5498cbfca785c@ruby-forum.com> (permalink)

Show all headers | View raw


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, well let me rephrase this in my eyes they are global
variables... but if I change them to mil_gal = 30 and fuel_gal = 10 they
actually work.

If what I have here are not global variables can someone explain this a
little bit?  I thought that by adding  @ it would make it global and
since I can use this inside other defs I was assuming that they were?

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)

Thanks a lot

-- 
Posted via http://www.ruby-forum.com/.

Back to comp.lang.ruby | Previous | NextNext in thread | Find similar | Unroll thread


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