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
38
39
40
# 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 do |selph|
      selph.__send__(symbol) if selph.respond_to?(symbol, true)
    end

    break nil if result.nil?
    result
  end
end

#try_chain!(*symbols) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/try_chain.rb', line 42

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

  symbols = symbols.flatten
  symbols.compact!

  symbols.reduce(self) do |result, symbol|
    result = result.try! do |selph|
      selph.__send__(symbol)
    end

    break nil if result.nil?
    result
  end
end