From: jake kaiden Date: 2011-04-15T12:30:28+09:00 Subject: Re: Understanding global variables. 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/.