Created
May 20, 2012 17:58
-
-
Save Mon-Ouie/2758930 to your computer and use it in GitHub Desktop.
This file contains 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
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