Sha256: 68f839d4880bc4f3b4ecd9c1ff038db775a852d2ed3a04a3f0cb249a2168eb0d

Contents?: true

Size: 974 Bytes

Versions: 4

Compression:

Stored size: 974 Bytes

Contents

# encoding: utf-8

class Object
  # *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.
  #
  # Invokes the method identified by the symbol +method+, passing it any arguments
  # and/or the block specified, just like the regular Ruby <tt>Object#send</tt> does.
  #
  # @author Botanicus
  # @since 0.0.3
  # @return [Object, nil] Return value of object.send(method) if object respond to method or nil
  # @param [Symbol] Method which will be called on object if object respond to this method
  # @param [Object, optional] Arguments for +method+ argument
  # @yield [block, optional] Block for +method+ argument
  # @example
  #   @post.try(:name) # instead @post && @post.name
  #   eBook.try(:find, 1)
  #   @post.try(:collect) { |p| p.name }
  def try(method, *args, &block)
    self.send(method, *args, &block) if self.respond_to?(method)
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rubyexts-0.3.pre lib/rubyexts/try.rb
rubyexts-0.0.2.1 lib/rubyexts/try.rb
rubyexts-0.0.2 lib/rubyexts/try.rb
rubyexts-0.1.pre lib/rubyexts/try.rb