Class: Object

Inherits:
BasicObject
Defined in:
lib/instance_call/core_ext/object.rb

Instance Method Summary collapse

Instance Method Details

#instance_call(m = nil, *args, &block) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/instance_call/core_ext/object.rb', line 3

def instance_call(m=nil, *args, &block)
  m ||= block
  case m
  when Symbol
    e = "#{m}".split('.')
    l = e.pop
    if e.empty?
      send(l, *args, &block)
    else
      r = e.inject(self) {|object, method| object.send(method) }
      r.send(l, *args, &block)
    end
  when Proc
    instance_exec(*args, &m)
  when UnboundMethod
    bound = m.bind(self)
    bound.call(*args, &block)
  when Method
    m.call(*args, &block)
  else
    raise ArgumentError, "Unsupported type #{m.class} and no block specified"
  end
end