Class: Module

Inherits:
Object show all
Defined in:
lib/nutella/core_ext/module.rb

Instance Method Summary collapse

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.

Examples:

Comparison of constructor creation with and without Nutella

# Pure Ruby
class Test
  def initialize(a, b, c, d)
    @a, @b, @c, @d = a, b, c, d
  end
end
Test.new(0, 0, 0, 0).instance_variables # => [:@a, :@b, :@c, :@d]

# Ruby with Nutella
class Test
  initializer *%i[a b c d]
end
Test.new(0, 0, 0, 0).instance_variables # => [:@a, :@b, :@c, :@d]

# It can also take a block with additional code to run
class Test
  initializer :a, :b do
    @in_block = 0
  end
end
Test.new(0, 0).instance_variables # => [:@a, :@b, :@in_block]

Parameters:

  • args (Array)

    the names of the arguments to the constructor as symbols

Yields:

  • code to be executed on construction



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