On Dec 18, 9:48=A0pm, Angel Marques <angel21... / hotmail.com> wrote:
> Hi..I'm learning rub..I know a bit about Python but I'm interested in
> use rails...I made a little program but this has a little bug too..for
> simplify this I dont post the program but I post a example
>
> if I declare
> var=3D'hi'
>
> def greeting
> =A0 =A0puts var
> end
>
> greeting()
> greting
>
> Do I need declare this like class variable necessarily?..inside my def
> ruby can't see outside?..
>
> ok..I declare this like a class variable...Now..I need a counter like
> this
>
> i=3D0
>
> loop {
> i+=3D1
> puts 'hi again'
>
> if i>100000: break end
>
> }
>
> why in this block I can see the outside value for i and inside the def
> block I can't??
>
> I should not use "standart" variables like variable instead use
> @@variabl??
>
> Please explain me this...and sorry for my poor english..I'm italian
> --
> Posted viahttp://www.ruby-forum.com/.

Class variables are for use like this:

Class Supah
def setVar(val)
@@var =3D val
end

def printVar
puts @@var
end
end

a =3D Supah.new
a.setVar 'hi'

b =3D Supah.new
b.printVar

=3D> "hi"

Class variables are simply variables that you can access from any
instance of the class.
To get a variable inside a def, you can pass it in like a parameter,
or you can use a global variable, a var prefixed by $.
In ruby, variables declared outside a function are not part of that
functions scope.
Hope this helps.