Class: Object

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

Instance Method Summary collapse

Instance Method Details

#try(*a, &b) ⇒ Object


14
15
16
# File 'lib/core_ext/object/try.rb', line 14

def try(*a, &b)
  try!(*a, &b) if a.empty? || respond_to?(a.first)
end

#try!(*a, &b) ⇒ Object


2
3
4
5
6
7
8
9
10
11
12
# File 'lib/core_ext/object/try.rb', line 2

def try!(*a, &b)
  if a.empty? && block_given?
    if b.arity.zero?
      instance_eval(&b)
    else
      yield self
    end
  else
    public_send(*a, &b)
  end
end