Method: Kernel#try

Defined in:
lib/garcon/core_ext/kernel.rb

#try(method = nil, *args, &block) ⇒ Object

Invokes the method identified by the symbol method, passing it any arguments and/or the block specified, just like the regular Ruby ‘Object#send` does.

Unlike that method however, a ‘NoMethodError` exception will not be raised and nil will be returned instead, if the receiving object is a `nil` object or NilClass.

For example, without try

@example = Struct.new(:name).new("bob")

@example && @example.name

or:

@example ? @example.name : nil

But with try

@example.try(:name)  #=> "bob"

or

@example.try.name    #=> "bob"

It also accepts arguments and a block, for the method it is trying:

@people.try(:collect){ |p| p.name }


113
114
115
116
117
118
119
# File 'lib/garcon/core_ext/kernel.rb', line 113

def try(method=nil, *args, &block)
  if method
    __send__(method, *args, &block)
  else
    self
  end
end