Created
May 14, 2013 09:58
-
-
Save mostlyobvious/5574905 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 InitializerStruct | |
def self.new(*args) | |
Class.new do | |
define_method(:initialize) do |*initializer_args| | |
arity_error = ArgumentError.new("wrong number of arguments (#{initializer_args.size} for #{args.size})") | |
raise arity_error unless initializer_args.size == args.size | |
args.each do |arg| | |
instance_variable_set("@#{arg}", initializer_args.shift) | |
end | |
end | |
end | |
end | |
end | |
class Dummy < InitializerStruct.new(:a, :b) | |
end | |
puts Dummy.new(1, 2).inspect | |
puts Dummy.new(1).inspect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment