Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #2909
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!news.mixmin.net!feeds.phibee-telecom.net!talisker.lacave.net!lacave.net!not-for-mail |
|---|---|
| From | jake kaiden <jakekaiden@yahoo.com> |
| Newsgroups | comp.lang.ruby |
| Subject | Re: Understanding global variables. |
| Date | Thu, 14 Apr 2011 22:30:28 -0500 |
| Organization | Service de news de lacave.net |
| Lines | 110 |
| Message-ID | <ce09a68a544e913e5e5107b45e537ad8@ruby-forum.com> (permalink) |
| 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 1302838255 33292 65.111.164.187 (15 Apr 2011 03:30:55 GMT) |
| X-Complaints-To | abuse@lacave.net |
| NNTP-Posting-Date | Fri, 15 Apr 2011 03:30:55 +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 | 381599 |
| X-Ml-Name | ruby-talk |
| X-Rubymirror | Yes |
| X-Ruby-Talk | <ce09a68a544e913e5e5107b45e537ad8@ruby-forum.com> |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.ruby:2909 |
Show key headers only | View raw
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