[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Class Level Variables

Cory

4/30/2007 12:05:00 AM

Alright, I'm missing some core ruby concept here that I just can't
figure out; any assistance would be excellent and welcome:

Long story short, I'm attempting to modify class level variables @@foo,
@@bar from an action in a ruby controller (an instance of a given
controller).

It seems that the class level variables set just fine...however, the
next time the action is invoked, they're nil again.

Code here: http://rafb.net/p/U3Q...

--
Posted via http://www.ruby-....

21 Answers

Ari Brown

4/30/2007 12:36:00 AM

0


On Apr 29, 2007, at 8:04 PM, Cory wrote:
<snip>

newbie question pertaining to class level variables:

What's the difference between @foo and @@foo? Is there even a
difference?

---------------------------------------------------------------|
~Ari
"I don't suffer from insanity. I enjoy every minute of it" --1337est
man alive




zswu

4/30/2007 12:44:00 AM

0

If you know C++.

@foo seams look link a normal member variable , i.e. private TYPE foo
@@foo seams look link a static member variable, i.e. static TYPE foo

Initialize the class variable in the constroctor is a good idea.
That's will make the class variable valide all of the class.
class xxx
def initialize
@@foo = XXXXX
end
end


On 4/30/07, Ari Brown <ari@aribrown.com> wrote:
>
> On Apr 29, 2007, at 8:04 PM, Cory wrote:
> <snip>
>
> newbie question pertaining to class level variables:
>
> What's the difference between @foo and @@foo? Is there even a
> difference?
>
> ---------------------------------------------------------------|
> ~Ari
> "I don't suffer from insanity. I enjoy every minute of it" --1337est
> man alive
>
>
>
>
>


--
/***********************************/
Lets go With the float....
/***********************************/

Gregory Brown

4/30/2007 1:00:00 AM

0

On 4/29/07, Ari Brown <ari@aribrown.com> wrote:
>
> On Apr 29, 2007, at 8:04 PM, Cory wrote:
> <snip>
>
> newbie question pertaining to class level variables:
>
> What's the difference between @foo and @@foo? Is there even a
> difference?

I wrote a newbie oriented post on this a while ago.
Hope it helps...

http://www.oreillynet.com/ruby/blog/2007/01/nubygems_dont_use_class_var...

Gregory Brown

4/30/2007 1:03:00 AM

0

On 4/29/07, zswu <wuzongsheng@gmail.com> wrote:
> If you know C++.
>
> @foo seams look link a normal member variable , i.e. private TYPE foo
> @@foo seams look link a static member variable, i.e. static TYPE foo
>
> Initialize the class variable in the constroctor is a good idea.
> That's will make the class variable valide all of the class.
> class xxx
> def initialize
> @@foo = XXXXX
> end
> end

Please don't do this. That assignment would happen every time you
create an instance of the object, which completely defeats the purpose
of class level variables. (Whether you use class instance variables or
class variables).

Class level variables are shared by all your instances.

Gregory Brown

4/30/2007 1:06:00 AM

0

On 4/29/07, Cory <coryw@americanmonkey.com> wrote:
> Alright, I'm missing some core ruby concept here that I just can't
> figure out; any assistance would be excellent and welcome:
>
> Long story short, I'm attempting to modify class level variables @@foo,
> @@bar from an action in a ruby controller (an instance of a given
> controller).

There is no such thing as a Ruby controller. I think you may actually
be asking a question about Ruby on Rails, so please use the rails
mailing list.

warm regards,
-gregory.

zswu

4/30/2007 1:20:00 AM

0

Huuuum, you are right.

On 4/30/07, Gregory Brown <gregory.t.brown@gmail.com> wrote:
> On 4/29/07, zswu <wuzongsheng@gmail.com> wrote:
> > If you know C++.
> >
> > @foo seams look link a normal member variable , i.e. private TYPE foo
> > @@foo seams look link a static member variable, i.e. static TYPE foo
> >
> > Initialize the class variable in the constroctor is a good idea.
> > That's will make the class variable valide all of the class.
> > class xxx
> > def initialize
> > @@foo = XXXXX
> > end
> > end
>
> Please don't do this. That assignment would happen every time you
> create an instance of the object, which completely defeats the purpose
> of class level variables. (Whether you use class instance variables or
> class variables).
>
> Class level variables are shared by all your instances.
>
>


--
/***********************************/
Lets go With the float....
/***********************************/

Cory

4/30/2007 1:25:00 AM

0

Gregory Brown wrote:
> On 4/29/07, Cory <coryw@americanmonkey.com> wrote:
>> Alright, I'm missing some core ruby concept here that I just can't
>> figure out; any assistance would be excellent and welcome:
>>
>> Long story short, I'm attempting to modify class level variables @@foo,
>> @@bar from an action in a ruby controller (an instance of a given
>> controller).
>
> There is no such thing as a Ruby controller. I think you may actually
> be asking a question about Ruby on Rails, so please use the rails
> mailing list.
>
> warm regards,
> -gregory.

You're right - it's a rails controller; but it's a ruby question.
Apparently my question has now been hijacked and we're now discussing
the difference between class level variables and instance level
variables.

Every test I've cooked out simply won't allow me to maintain state in a
class level variable set from an instance level context. That's the
core of the question.


--
Posted via http://www.ruby-....

Gary Wright

4/30/2007 1:32:00 AM

0


On Apr 29, 2007, at 8:44 PM, zswu wrote:

> If you know C++.
>
> @foo seams look link a normal member variable , i.e. private TYPE foo
> @@foo seams look link a static member variable, i.e. static TYPE foo

I think a lot of confusion about Ruby class variables comes from
trying to compare them to similarly named constructs in other languages.

There is also a tendency to think class variables are like instance
variables because @@ is like @. Again, you'll be mislead by that
assumption.

1) Class variables are lexically scoped. They are *not* associated
with the object identified by 'self' but instead by the innermost
class block:

class B;end
class A
@@foo = 42 # @@foo for A
def foo
@@foo # @@foo for A
end
def B.foo
@@foo # self is B but @@foo is for A
end
end

puts A.new.foo # 42
puts B.foo # 42

This lexical scoping is really important to understand when you
start using singleton class blocks, class_eval, module_eval,
or define_method, since the these constructs don't introduce a lexical
scope:

class X
@@foo = 42
class Y
@@foo = 43
X.class_eval {
puts @@foo # 43, this is Y's @@foo, not X's @@foo
}
end
end

2) Class variables are associated with a class *and* its subclasses
and the order of initialization is important.

class C
@@foo = 42 # shared by C and its decendents!
def foo
@@foo # shared with C, D, and E
end
def bar
@@bar # this is C's @@bar
end
end

class D < C
end

class E < C
@@foo = 43 # @@foo shared with C, D, and E
@@bar = 44 # @@bar only defined for E
def bar
@@bar # this is E's @@bar
end
end

puts D.new.foo # 43

puts E.new.bar # 44
puts C.new.bar # undefined!

class C
@@bar = 45
end

puts C.new.bar # 45
puts E.new.bar # still 44


> Initialize the class variable in the constroctor is a good idea.
> That's will make the class variable valide all of the class.
> class xxx
> def initialize
> @@foo = XXXXX
> end
> end

This is a really bad idea. Every time you create a new instance,
you'll reset the class variable.


Gary Wright




Cory

4/30/2007 1:45:00 AM

0

Cory wrote:
> Alright, I'm missing some core ruby concept here that I just can't
> figure out; any assistance would be excellent and welcome:
>
> Long story short, I'm attempting to modify class level variables @@foo,
> @@bar from an action in a ruby controller (an instance of a given
> controller).
>
> It seems that the class level variables set just fine...however, the
> next time the action is invoked, they're nil again.
>
> Code here: http://rafb.net/p/U3Q...

It just struck me - it likely is a rails issue, I'm running in
development mode and methinks it's likely reloading the class every
single time.

And - sorry if I seemed snippy, been fighting this for a while.

--
Posted via http://www.ruby-....

Gregory Brown

4/30/2007 1:58:00 AM

0

On 4/29/07, Cory Wilkerson <coryw@americanmonkey.com> wrote:
> Cory wrote:
> > Alright, I'm missing some core ruby concept here that I just can't
> > figure out; any assistance would be excellent and welcome:
> >
> > Long story short, I'm attempting to modify class level variables @@foo,
> > @@bar from an action in a ruby controller (an instance of a given
> > controller).
> >
> > It seems that the class level variables set just fine...however, the
> > next time the action is invoked, they're nil again.
> >
> > Code here: http://rafb.net/p/U3Q...
>
> It just struck me - it likely is a rails issue, I'm running in
> development mode and methinks it's likely reloading the class every
> single time.
>
> And - sorry if I seemed snippy, been fighting this for a while.

As far as I know, it's a rails issue period. Unless you're storing
something in the database(either via sessions or models), you don't
have state preservation. I don't know enough about Rails to give you
the information you need about that, others on this list do.

However, the best place to ask this is certainly the Rails list.