Class: Object
- Inherits:
- BasicObject
- Defined in:
- lib/factory_data_preloader/core_ext.rb
Instance Method Summary collapse
-
#try(method, *args, &block) ⇒ Object
Tries to send the method only if object responds to it.
Instance Method Details
#try(method, *args, &block) ⇒ 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 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}
19 20 21 |
# File 'lib/factory_data_preloader/core_ext.rb', line 19 def try(method, *args, &block) send(method, *args, &block) if respond_to?(method, true) end |