Understanding global variables.

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
 
7

7stud --

Fily Salas wrote in post #992901:
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,

Wrong. In ruby, global variables start with a $ sign and they are
visible everywhere(that is except for the regex 'global' variables that
ruby predefines)
well let me rephrase this in my eyes they are global
variables...

You may think those variables are global to the class, i.e. they can be
referred to in any method definition--but they cannot. "Instance
variables", the ones that start with '@' attach themselves to whatever
object is self at the time they are created, and inside a class
definition but outside of any method definitions self is equal to the
class:

class A
puts self
end

--output:--
A

In your case, the instance variables attach to the class, and therefore
you must use the class as the receiver:

Car.mil_gal

Those are known as "class instance variables" and are preferred over
class variables, e.g. @@mil_gal.

A variable that is visible in all method definitions within a class is
called an "instance variable", and it begins with "@" and can be created
inside a method definition to get the behaviour you want. Inside a
method definition, self is equal to the object that called the method,
so the instance variable attaches to the object.

In ruby, classes carry the common methods of all instances of the class,
and instances each have their own instance variables.
 
J

jake kaiden

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
 
F

Fily Salas

Wow, a little confused, I=E2=80=99m a little disappointed that the langua=
ge its =

self seems to be easer but a simple concept as variables seems a little =

complicated (Its probably just me).

This may be confusing because I have never heard about =E2=80=9CSELF=E2=80=
=9D statement, =

=E2=80=9CINITIALZE=E2=80=9D method and the =E2=80=9C:=E2=80=9D notation, =
I guess I need to read more =

about the language.

To make this short what I was trying to say with GLOBAL is accessible in =

all methods within the class because I now remember reading what 7stud =

said (do not use global variables) in a book.

What would happen if I position a variable with the wrong notation? In =

other words if I position a variable where an instance variable would =

normally go but without the @ will Ruby get confused and treat this =

differently and may get an error or the interpreter will simply use it =

and keep track of what kind of variable it is by itself.

Any good tutorial about variables in Ruby?

Sorry I=E2=80=99m confused and sorry if I=E2=80=99m confusing you guys,

Thank you for your patience

-- =

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

Fily Salas

Thanks for the links I will read them and I'm sure I come back with some
other question.

I hope you guys don't mind answering boring questions like the ones I
usually ask... :(

Thanks a lot
 
J

jake kaiden

no problem - and don't worry, the forum is for asking questions. sorry
also if my first reply was overwhelming, might have been too much
information!

-j
 
7

7stud --

Fily Salas wrote in post #992993:
This may be confusing because I have never heard about =E2=80=9CSELF=E2= =80=9D statement,
=E2=80=9CINITIALZE=E2=80=9D method and the =E2=80=9C:=E2=80=9D notation= , I guess I need to read more
about the language.

Knowing which object is 'self' is sort of an intermediate topic, but it =

is critical to understanding how ruby works.
What would happen if I position a variable using the wrong syntax? In
other words if I position a variable where an instance variable would
normally go but without the @ will Ruby get confused and treat this
differently and may get an error or the interpreter will simply use it
and keep track of what kind of variable it is by itself.

A variable name that is not preceded by an '@', is called a 'local =

variable', and a local variable ceases to exist once the method ends. =

Here is an example:

class Dog
def initialize(a_name, a_color)
@name =3D a_name
color =3D a_color
end

def name
@name #same as 'return @name'
end

def name=3D(str)
@name =3D str
end

def color
@color #same as 'return @color'
end


end

my_dog =3D Dog.new('Spot', 'black')
#Calling new() automatically causes initialize()
#to execute.

puts my_dog.name #=3D> Spot
my_dog.name =3D 'Max'
puts my_dog.name #=3D> Max

Any good tutorial about variables in Ruby?

"Beginning Ruby (2nd ed)" by Cooper

-- =

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

Fily Salas

Thank you all very much!

I would lie if I say that I now understand everything you guys have
explained but a lot of thing make a lot more sense.

Also part of the confusion is that in other languages like Actionscript
3.0 you define your instance variables outside the methods and I was
expecting the same concept in Ruby where variables inside methods cannot
be accessed from outside code.

I will be reading more about the language.

Learning a lot!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top