Last active
July 19, 2017 03:47
-
-
Save noel9999/253abeb3663064d81491fc95a1605ecf to your computer and use it in GitHub Desktop.
Class Instance Variable example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class A | |
@obj ||= Object.new | |
def self.obj | |
@obj | |
end | |
def self.inherited(subclass) | |
puts 'Hello inherited' | |
puts "self: #{self}" | |
puts "self obj_id: #{@obj.object_id}" | |
puts "subclass: #{subclass}" | |
subclass.instance_exec(@obj) do |obj| | |
puts "Hello object_id: #{obj.object_id}" | |
@obj = obj | |
end | |
end | |
end | |
class B < A; end | |
A.obj == B.obj | |
A.obj.equal? B.obj | |
B.instance_eval { @obj = Object.new } | |
A.obj == B.obj | |
A.obj.equal? B.obj |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment