Sha256: 0ad490618e2d83a5a1eaa52ce6de479651990f749065836f5843f393f82ea16e

Contents?: true

Size: 783 Bytes

Versions: 16

Compression:

Stored size: 783 Bytes

Contents

# http://github.com/rails/rails/tree/823b623fe2de8846c37aa13250010809ac940b57/activesupport/lib/active_support/core_ext/object/misc.rb

unless Object.respond_to?(:try) # Object#try is in Rails 2.3 but not in 2.2.
  class Object
    # Tries to send the method only if object responds to it. Return +nil+ otherwise.
    # It will also forward any arguments and/or block like Object#send does.
    # 
    # ==== Example :
    # 
    # # Without try
    # @person ? @person.name : nil
    # 
    # With try
    # @person.try(:name)
    #
    # # try also accepts arguments/blocks for the method it is trying
    # Person.try(:find, 1)
    # @people.try(:map) {|p| p.name}
    def try(method, *args, &block)
      send(method, *args, &block) if respond_to?(method, true)
    end
  end
end

Version data entries

16 entries across 16 versions & 2 rubygems

Version Path
myronmarston-factory_data_preloader-0.1.0 lib/factory_data_preloader/core_ext.rb
myronmarston-factory_data_preloader-0.2.0 lib/factory_data_preloader/core_ext.rb
myronmarston-factory_data_preloader-0.3.0 lib/factory_data_preloader/core_ext.rb
myronmarston-factory_data_preloader-0.3.1 lib/factory_data_preloader/core_ext.rb
myronmarston-factory_data_preloader-0.3.2 lib/factory_data_preloader/core_ext.rb
myronmarston-factory_data_preloader-0.4.0 lib/factory_data_preloader/core_ext.rb
myronmarston-factory_data_preloader-0.4.1 lib/factory_data_preloader/core_ext.rb
myronmarston-factory_data_preloader-0.4.2 lib/factory_data_preloader/core_ext.rb
myronmarston-factory_data_preloader-0.4.3 lib/factory_data_preloader/core_ext.rb
myronmarston-factory_data_preloader-0.5.0 lib/factory_data_preloader/core_ext.rb
myronmarston-factory_data_preloader-0.5.1 lib/factory_data_preloader/core_ext.rb
myronmarston-factory_data_preloader-0.5.2 lib/factory_data_preloader/core_ext.rb
factory_data_preloader-1.0.3 lib/factory_data_preloader/core_ext.rb
factory_data_preloader-1.0.2 lib/factory_data_preloader/core_ext.rb
factory_data_preloader-1.0.1 lib/factory_data_preloader/core_ext.rb
factory_data_preloader-0.5.2 lib/factory_data_preloader/core_ext.rb