Created
June 11, 2013 07:48
-
-
Save moro/5755105 to your computer and use it in GitHub Desktop.
Coffee script like constructor definition
This file contains hidden or 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
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