Class: Loverload::Method

Inherits:
Object
  • Object
show all
Defined in:
lib/loverload/method.rb

Instance Method Summary collapse

Constructor Details

#initialize(klass, method_name, &with_params_block) ⇒ Method

Returns a new instance of Method.



3
4
5
6
7
8
9
# File 'lib/loverload/method.rb', line 3

def initialize klass, method_name, &with_params_block
  @klass, @method_name = klass, method_name

  dictionary || @klass.instance_variable_set(:@__dictionary__, {})

  instance_eval(&with_params_block)
end

Instance Method Details

#overload(instance, *args, &block) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/loverload/method.rb', line 17

def overload instance, *args, &block
  if block_given?
    args << Block.new(&block)
    found = find(*args)
    instance.instance_exec *args do |*args|
      found.call(*args, &(args.pop.block))
    end
  else
    instance.instance_exec *args, &find(*args)
  end
end

#with_params(*pars, &block) ⇒ Object



11
12
13
14
15
# File 'lib/loverload/method.rb', line 11

def with_params *pars, &block
  arg_count = block.arity
  arg_count += 1 if pars.last == ::Loverload::Block
  dictionary[method_signature(arg_count, *pars)] = block
end