Skip to content

Instantly share code, notes, and snippets.

@Mon-Ouie
Created May 20, 2012 17:58
Show Gist options
  • Save Mon-Ouie/2758930 to your computer and use it in GitHub Desktop.
Save Mon-Ouie/2758930 to your computer and use it in GitHub Desktop.
class Module
def method_default(*method_names)
method_names.each do |name|
method = instance_method(name)
ivars = method.parameters.map { |type, name| "@#{name}" }
define_method(name) do |*args, &block|
ivars.zip(args) do |name, arg|
instance_variable_set name, arg
end
method.bind(self).call(*args, &block)
end
end
end
end
class Foo
def initialize(foo, bar, baz)
p @foo, @bar, @baz
end
method_default :initialize
end
Foo.new(3, 4, 5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment