76

I just learned about static variables in php. Is there anything like that in ruby?

For example, if we want to create a Student class and for each student object we create, its id number should get incremented automatically.

I thought creating class variable as a static will do.

Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
levirg
  • 1,033
  • 2
  • 8
  • 9

2 Answers2

118

Class variables are shared between all instances (which is why they're called class variables), so they will do what you want. They're also inherited which sometimes leads to rather confusing behavior, but I don't think that will be a problem here. Here's an example of a class that uses a class variable to count how many instances of it have been created:

class Foo
  @@foos = 0

  def initialize
    @@foos += 1
  end

  def self.number_of_foos
    @@foos
  end
end

Foo.new
Foo.new
Foo.number_of_foos #=> 2
dee-see
  • 23,668
  • 5
  • 58
  • 91
sepp2k
  • 363,768
  • 54
  • 674
  • 675
  • Thanks man..I did something idiotic in my code, and totally misunderstood the concept.... – levirg Mar 10 '10 at 12:39
  • -1 Your example is has a downside, foos from `@@foos = 0` is not the same variable with the other two, its a it's a class variable of the class `Class` (Foo is an instance of class Class), i'll remove the downvote if you correct it. – clyfe Mar 10 '10 at 17:44
  • 6
    @clyfe: You're wrong. You're confusing class variables with instance variables. Doing `class Foo; @@foo = "foo" end` sets the class variable `@@foo` for Foo, not for Class. Try it: `class Foo; @@foo = 1 end; class Object; @@foo end` will produce a `NameError: uninitialized class variable @@foo in Class`. – sepp2k Mar 10 '10 at 18:05
  • You are right, i'm sorry, i can't remove the downvote, unless you edit some. Shouldn't the `@, @@` notation follow the current `self` scope? – clyfe Mar 10 '10 at 19:10
  • @clyfe: You can remove the downvote now. I just fixed a typo. As for @@ following the value of self: I suppose that would be more consistent, but then you wouldn't have a place to initialize class variables. Also class-methods wouldn't be able to access class variables, which means you'd have to have access to an instance of Foo to ask for the number of foos that have been created in the example in my answer. – sepp2k Mar 10 '10 at 20:27
  • I see strong trend in Ruby community to favour class instance variables to class variables, as it seems that class variables has unpredictable behavior... – khelll Mar 13 '10 at 08:10
  • 2
    @khelll: Not unpredictable, unexpected. The behavior of class variables is completely deterministic. – sepp2k Mar 13 '10 at 12:31
  • how about this: @@v = 1 ; class MyClass ; @@v = 2 ;end ;@@v #=>2 – khelll Mar 13 '10 at 13:13
  • 1
    @khell: What about it? I already mentioned that class variables are inherited. I also said that that leads to confusing behavior. Still this code will return the same result every time you run it. And even if you hadn't mentioned that it returns 2, I would have known that it returns 2 without running it, so clearly it's deterministic. – sepp2k Mar 13 '10 at 13:28
  • yea sorry, I failed to pick the proper word, so it's **unexpected**. Anyway, I'm just trying to emphasize that class variables have confusing behavior. Still they are the closest to 'static' variables. Also I recommend using class instance variables cause it's more Rubyish(as I believe). – khelll Mar 13 '10 at 13:58
18

Using the accepted answer as the definition of static variable can be dangerous, and it is a common error that I have seen in a lot of Ruby code.

Something like @@foos is shared among ALL subclasses. However, most programmers expect static variables to have scope only within the class where they are defined.

If you are looking for static variables in the sense of most languages, where their scope is only their own class, look at this SO answer

Also this blog post has a nice example of the surprise most programmers will get:

http://www.railstips.org/blog/archives/2006/11/18/class-and-instance-variables-in-ruby/

Community
  • 1
  • 1
SystematicFrank
  • 16,555
  • 7
  • 56
  • 102
  • 3
    In fact you'll be surprised about the number of people with misconception expecting static to be shared among all subclasses in other languages :P – Rael Gugelmin Cunha Feb 20 '15 at 11:41