Skip to content

Instantly share code, notes, and snippets.

@jamesshipton
Last active August 29, 2015 14:06
Show Gist options
  • Save jamesshipton/06cef23cb4bef1578e01 to your computer and use it in GitHub Desktop.
Save jamesshipton/06cef23cb4bef1578e01 to your computer and use it in GitHub Desktop.
Ruby Object Model
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