Skip to content

Instantly share code, notes, and snippets.

@gr33n7007h
Created December 25, 2019 01:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gr33n7007h/f976eccb32cfeb98f1ed69c026252cb3 to your computer and use it in GitHub Desktop.
Save gr33n7007h/f976eccb32cfeb98f1ed69c026252cb3 to your computer and use it in GitHub Desktop.
blah
#!/usr/bin/env ruby
class R
def self.val
@val ||= :bad
end
def self.val= o
@val = o
end
end
# This way creates an anonymous class.
class A < Class.new(R) { |x| x.val = :good }; end
# This way doesn't
B = Class.new(R) { |x| x.val = :good }
# Let's peek at ancestors
p A.ancestors #=> [A, #<Class:0x0000565135ce74d0>, R, Object, Kernel, BasicObject]
p B.ancestors #=> [B, R, Object, Kernel, BasicObject]
# A
p A.val #=> :bad
# #<Class:0x0000565135ce74d0>
p A.superclass.val #=> :good
# R
p A.superclass.superclass.val #=> :bad
# R
p B.val #=> good
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment