Ruby / Bugs

From WhyNotWiki

Jump to: navigation, search

All: http://rubyforge.org/tracker/?atid=1698&group_id=426&func=browse

[edit] [#11455] class variable for self shares same object as class variable for subclass

http://rubyforge.org/tracker/index.php?func=detail&aid=11455&group_id=426&atid=1698

http://svn.tylerrick.com/public/examples/ruby/bugs/class_variable_for_self_shares_same_object_as_class_variable_for_subclass.rb

class A
  @@v = 'a'   # If we initialize it here first, it causes A and B to share the same object for @@v !
end

class B < A
  @@v = 'b'
end

puts A.send(:class_variable_get, :@@v).object_id # => -604548898
puts B.send(:class_variable_get, :@@v).object_id # => -604548898 -- Same object -- *not* what I would expect!

puts A.send(:class_variable_get, :@@v)  # => 'b' (expected 'a')
puts B.send(:class_variable_get, :@@v)  # => 'b'


#---------------------------------------------------------------------------------------------------------------------------------
# But if we do it like this...
class C
end

class D < C
  @@v = 'b'
end

class C
  @@v = 'a'
end

# C's and D's @@v correctly refer to different objects...
puts C.send(:class_variable_get, :@@v).object_id # => -604549028
puts D.send(:class_variable_get, :@@v).object_id # => -604549008 -- Different objects -- that's what I would expect!

puts C.send(:class_variable_get, :@@v)  # => 'a'
puts D.send(:class_variable_get, :@@v)  # => 'b'


Bugs  edit   (Category  edit)

Personal tools