Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #2909
| From | jake kaiden <jakekaiden@yahoo.com> |
|---|---|
| Newsgroups | comp.lang.ruby |
| Subject | Re: Understanding global variables. |
| Date | 2011-04-14 22:30 -0500 |
| Organization | Service de news de lacave.net |
| Message-ID | <ce09a68a544e913e5e5107b45e537ad8@ruby-forum.com> (permalink) |
| References | <962f80bb11f2292d2ba5498cbfca785c@ruby-forum.com> |
hi fily,
as 7stud says, the "@" variables are not global, but instance
variables - which means you can pass them between methods (very handy.)
from what i gather of what you're trying to do with what you've written,
i don't think you actually need instance variables at all, take a look
at this for example:
#####
class Car
def go(mil_gal, fuel_gal)
mileage = mil_gal * fuel_gal
puts mileage
end
end
car = Car.new
car.go(10, 50)
#=> 500
#####
or you could do something like this:
#####
class Car
def initialize(mil_gal, fuel_gal)
mileage = mil_gal * fuel_gal
puts mileage
end
end
car = Car.new(10, 50)
#=> 500
car = Car.new(30,70)
#=> 2100
###
if you want to use instance variables, you can do something like this:
#####
class Car
def initialize
@mil_gal = 10
@fuel_gal = 50
end
def go(mil_gal, fuel_gal)
@mil_gal = mil_gal
@fuel_gal = fuel_gal
self.mileage
end
def mileage
mileage = @mil_gal * @fuel_gal
puts mileage
end
end
car = Car.new
car.mileage
#=> 500
car.go(30, 70)
#=> 2100
####
and if you want to pass those instance variables outside of the class,
and be able to change them, you can do something like this:
#####
class Car
attr_accessor :mil_gal, :fuel_gal, :mileage
def initialize
init_mil_gal = 10
init_fuel_gal = 50
self.go(init_mil_gal, init_fuel_gal)
end
def go(mil_gal, fuel_gal)
@mil_gal = mil_gal
@fuel_gal = fuel_gal
end
def mileage
@mileage = @mil_gal * @fuel_gal
end
end
car = Car.new
puts car.mil_gal, car.fuel_gal, car.mileage
puts
car.go(30, 70)
puts car.mil_gal, car.fuel_gal, car.mileage
car.mil_gal = 38
puts
puts car.mil_gal
puts car.mileage
#####
drive on,
- j
--
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