Re: static and class variable?

Thorsten Müller
Tue, 01 Jul 2008 11:17:07 -0000

It''s different

class variables are variables of the class
Their value is shared by all objects

Class Something
  @@class_variable = 0

  def do_something
    @@class_variable +=1
  end
end

foo = Something.new
foo.do_something
bar = Something.new
bar.do_something

After that code @@class_variable will be 2
It behaves like a static class variable in C++

There are no static variables on a function level like in C++ as far
as I know
You can use normal class attributes or class variables instead
(depending if you want them per object or not)

Here is some longer discussion on the topic:
http://groups.google.com/group/ruby-talk-google/browse_thread/thread/30c79cd4b9fb5c8
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---