Created
July 12, 2010 19:43
-
-
Save aarongough/472964 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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