# B class redefine class variable class A @@class_var = 'first' def first @@class_var end end class B < A @@class_var = 'second' def second @@class_var end end p B.new.first # => second p B.new.second # => second p B.singleton_methods # => [] # A and B class have different variable # In this case we have instance(singleton) variable for A and B class A @class_var = 'first' class << self attr_accessor :class_var end def first A.class_var end end class B < A @class_var = 'second' class << self attr_accessor :class_var end def second B.class_var end end p B.new.first # => first p B.new.second # => second p B.singleton_methods # => [:class_var, :class_var=]
Saturday, June 29, 2013
Ruby class variables
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment