Method: Object#try

Defined in:
lib/ruby/jruby_hack.rb

#try(*args, &block) ⇒ Object

Sends the arguments to ‘self` or yields `self` (when `self` is non-`nil`). This is overridden by NilClass#try, which always returns `nil`.

Examples:

"non-nil".try(&:length)       #=> 7
nil.try(&:length)             #=> nil

"non-nil".try(:slice, 0, 3)   #=> "non"
nil.try(:slice, 0, 3)         #=> nil


657
658
659
660
661
662
663
# File 'lib/ruby/jruby_hack.rb', line 657

def try(*args, &block)
  if args.empty?
    yield self
  else
    __send__(*args, &block)
  end
end