F
Fily Salas
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
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