Skip to content

Instantly share code, notes, and snippets.

@aarongough
Created July 12, 2010 19:43
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save aarongough/472964 to your computer and use it in GitHub Desktop.
Save aarongough/472964 to your computer and use it in GitHub Desktop.
blank_object = {
:parent => nil,
:slots => {},
:size => 0
}
def send(receiver, message, *params, &block)
method_owner = receiver
while(method_owner[:slots][:lookup].nil?)
puts "ERR: lookup failed for ':lookup' on object:\n#{receiver.inspect}" and break if(method_owner[:parent].nil?)
method_owner = method_owner[:parent]
end
method = method_owner[:slots][:lookup].call(receiver, message)
exit if(method.nil?)
method.call(receiver, params << block)
end
def derive_from(object)
child = {}
child[:parent] = object
child[:size] = 0
child[:slots] = {}
return child
end
basic_object = derive_from(blank_object)
basic_object[:slots][:lookup] = Proc.new do |this, message|
method_owner = this
while(!method_owner.nil? && method_owner[:slots][message.to_sym].nil?)
puts "ERR: lookup failed for '#{message}' on object:\n#{this.inspect}" and break if(method_owner[:parent].nil?)
method_owner = method_owner[:parent]
end
method_owner[:slots][message.to_sym] unless(method_owner.nil?)
end
basic_object[:slots][:add_method] = Proc.new do |this, params|
puts "ERR: add_method called without method key" and return unless(params.first.is_a?(String) || params.first.is_a?(Symbol))
puts "ERR: add_method called without block" and return unless(params.last.is_a?(Proc))
this[:slots][params.first.to_sym] = params.last
this[:size] += 1
end
basic_object[:size] = 2
object_with_size = derive_from(basic_object)
send(object_with_size, :add_method, :size) do |this, params|
this[:size]
end
puts send(object_with_size, :size)
blah = derive_from(object_with_size)
puts send(blah, :size)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment