tags:

views:

318

answers:

1

i just learned about static variables in php.. i was just wondering is there any thing like that in ruby...

say..

if we want to create a student class and for each student(object) we create, its id no should get incremented automatically..

I thought creating class variable as a static will do...

thanks,

regards, levirg

+4  A: 

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 man 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
sepp2k
Thanks man..I did something idiotic in my code, and totally misunderstood the concept....
levirg
-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
@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
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
@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
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
@khelll: Not unpredictable, unexpected. The behavior of class variables is completely deterministic.
sepp2k
how about this: @@v = 1 ; class MyClass ; @@v = 2 ;end ;@@v #=>2
khelll
@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
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