Forum: Ruby on Rails how can I access a class variable outside of the class???

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
sayoyo S. (Guest)
on 2006-04-25 20:26
(Received via mailing list)
Hi

we wanted to access a class varaible from outside of
the same class, like

class klass
   @@var1
end

we found out we can not use the class variable like
this:
klass.var1 we have to create a class function like


class klass
   @@var1

   def var1
     return @@var1
   end

end


If anyone know how to access a class variable directly
without passing by a function???

Thanks you very much

Saiho



The mind is its own place, and in itself.
Can make a Heaven of Hell, a Hell of Heaven.

http://www.geocities.com/sayoyo/

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
Hemant K. (Guest)
on 2006-04-26 00:26
(Received via mailing list)
>From picaxe book:

" Class variables are private to a class and its instances. If you want
to
make them accessible to the outside world, you'll need to write an
accessor
method. This method could be either an instance method or, leading us
neatly
to the next section, a class method. "
Alex W. (Guest)
on 2006-04-26 00:52
Saiho sayoyo wrote:
> Hi
>
> we wanted to access a class varaible from outside of
> the same class, like
>
> class klass
>    @@var1
> end


Try

cattr_accessor :some_var

works just like attr_accessor except for class variables.
This topic is locked and can not be replied to.