Skip to content

Instantly share code, notes, and snippets.

@moro
Created June 11, 2013 07:48
Show Gist options
  • Save moro/5755105 to your computer and use it in GitHub Desktop.
Save moro/5755105 to your computer and use it in GitHub Desktop.
Coffee script like constructor definition
module CoffeeConstructor
def constructor(*ivars)
define_method :initialize do |*args|
raise(ArgumentError) unless ivars.size == args.size
ivars.zip(args).each do |ivar, val|
instance_variable_set(ivar, val)
end
end
end
end
class Foo
extend CoffeeConstructor
constructor :@foo, :@bar
def hi
puts "hello #{@foo} and #{@bar}"
end
end
Foo.new('alice', 'bob').hi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment