Module: TryChain::InstanceMethods

Included in:
Object
Defined in:
lib/try_chain.rb

Instance Method Summary collapse

Instance Method Details

#try_chain(*symbols) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/try_chain.rb', line 26

def try_chain(*symbols)
  return nil if self.nil?

  symbols = symbols.flatten
  symbols.compact!

  symbols.reduce(self) do |result, symbol|
    result = result.try(symbol)
    break nil if result.nil?
    result
  end
end

#try_chain!(*symbols) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/try_chain.rb', line 39

def try_chain!(*symbols)
  return nil if self.nil?

  symbols = symbols.flatten
  symbols.compact!

  symbols.reduce(self) do |result, symbol|
    result = result.try!(symbol)
    break nil if result.nil?
    result
  end
end