Class: Module
Instance Method Summary collapse
-
#initializer(*args) { ... } ⇒ Object
A DRYer way to create a constructor where all of its arguments are assigned to instance variables of the same name.
Instance Method Details
#initializer(*args) { ... } ⇒ Object
A DRYer way to create a constructor where all of its arguments are assigned to instance variables of the same name.
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/nutella/core_ext/module.rb', line 30 def initializer(*args, &block) define_method :initialize do |*input| (input.last.is_a?(Hash) \ ? input[-1] : Hash[args.zip input]).each do |name, value| instance_variable_set "@#{name}", value end instance_eval(&block) if block_given? end end |