Thursday, October 13, 2011

Class level variables in ruby

We should use @@ to create such variables, they are similar to static variables we see in java or cpp.

class MyClass

@@count = 0 # Initialize a class variable

def initialize # called when object is allocated
@@count += 1
@myvar = 10
end
end

No comments:

Post a Comment