In rails, What is the difference between static and class variable? Is it the same or different?
Thanks, Ratnavel
In rails, What is the difference between static and class variable? Is it the same or different?
Thanks, Ratnavel
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