Skip to content

Instantly share code, notes, and snippets.

@jdjkelly
Created July 7, 2014 03:21
Show Gist options
  • Save jdjkelly/e517ba31c1f4c007651b to your computer and use it in GitHub Desktop.
Save jdjkelly/e517ba31c1f4c007651b to your computer and use it in GitHub Desktop.
class ObjectBuilder
def prototype(proto)
# Leave method implementations on the proto object
ivars = proto.instance_variables.reject{ |ivar_name|
proto.respond_to?(ivar_name.to_s[1..-1]) &&
proto.instance_variable_get(ivar_name).is_a?(Proc)
}
ivars.each do |ivar_name|
unless @object.instance_variable_defined?(ivar_name)
@object.instance_variable_set(
ivar_name,
proto.instance_variable_get(ivar_name).dup)
end
end
@object.extend(Prototype.new(proto))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment