Class: Object

Inherits:
BasicObject
Defined in:
lib/ruby-try.rb

Instance Method Summary collapse

Instance Method Details

#try(*a, &b) ⇒ Object



2
3
4
5
6
7
8
# File 'lib/ruby-try.rb', line 2

def try(*a, &b)
  if a.empty? && block_given?
    yield self
  else
    public_send(*a, &b) if respond_to?(a.first)
  end
end

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

Same as #try, but will raise a NoMethodError exception if the receiving is not nil and does not implement the tried method.



12
13
14
15
16
17
18
# File 'lib/ruby-try.rb', line 12

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

#try?(*a, &b) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ruby-try.rb', line 20

def try?(*a, &b)
  if a.empty? && block_given?
    yield self
  else
    if respond_to?(a.first)
      public_send(*a, &b)
    else
      nil.try?(*a, &b)
    end
  end
end