Module: Konstructor::KonstructorMethod

Defined in:
lib/konstructor/main.rb

Instance Method Summary collapse

Instance Method Details

#konstructor(*several_variants) ⇒ Object

konstructor -> nil or konstructor(symbol, …) -> nil or konstructor(string, …) -> nil

If used without params, declares next method as constructor.

module SomeClass
  attr_reader :val

  konstructor
  def create(val)
    @val = val
  end
end

If names are given, call can be placed anywhere, only methods with those names will be declared as constructors.

module SomeClass
  attr_reader :val

  def create(val)
    @val = val
  end

  konstructor :create, :recreate

  def recreate(val)
    @val = val * 2
  end
end

then:

SomeClass.new.val
=> nil
SomeClass.create(3).val
=> 3
SomeClass.recreate(3).val
=> 6

Can be used multiple times with various arguments, all calls add up without overwriting each other.

Can raise several errors inheriting from Konstructor::Error:

ReservedNameError
DeclaringInheritedError
IncludingInModuleError


61
62
63
64
# File 'lib/konstructor/main.rb', line 61

def konstructor(*several_variants) # :doc:
  Konstructor.declare(self, several_variants)
  nil
end