Skip to content

Instantly share code, notes, and snippets.

@noel9999
Last active July 19, 2017 03:47
Show Gist options
  • Save noel9999/253abeb3663064d81491fc95a1605ecf to your computer and use it in GitHub Desktop.
Save noel9999/253abeb3663064d81491fc95a1605ecf to your computer and use it in GitHub Desktop.
Class Instance Variable example
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