Last active
August 29, 2015 14:06
-
-
Save jamesshipton/06cef23cb4bef1578e01 to your computer and use it in GitHub Desktop.
Ruby Object Model
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
MyModule = Module.new | |
MyModule.module_eval do | |
@module_ivar = 4 | |
class << self | |
def my_singleton_method | |
@module_ivar | |
end | |
end | |
def my_module_method | |
end | |
end | |
MyClass = Class.new | |
MyClass.class_eval do | |
include MyModule | |
@class_ivar = 0 | |
def initialize(ivar) | |
@ivar = ivar | |
end | |
class << self | |
def my_singleton_method | |
@class_ivar | |
end | |
end | |
def my_instance_method | |
@ivar | |
end | |
end | |
object_1 = MyClass.new(1) | |
object_2 = MyClass.new(2) | |
object_2.instance_eval do | |
@another_ivar = 3 | |
def my_singleton_method | |
@another_ivar | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment