Module: ReactiveExtensions
- Included in:
- Object
- Defined in:
- lib/extensions/reactive_extensions.rb,
lib/reactive_support/extensions/reactive_extensions.rb
Overview
The ReactiveExtensions module consists of methods I wish ActiveSupport provided. These methods do not adhere to the ActiveSupport API. If you wish to include them in your project, you will need to put this in your main project file:
require 'reactive_support/extensions'
ReactiveExtensions requires ReactiveSupport, so there is no need to require both explicitly.
Instance Method Summary collapse
-
#try_rescue(*args, &block) ⇒ Object
The
#try_rescuemethod extends ReactiveSupport’s#trymethod so it rescues NoMethodErrors and TypeErrors as well as returningnilwhen called on anilvalue.
Instance Method Details
#try_rescue(*args, &block) ⇒ Object
The #try_rescue method extends ReactiveSupport’s #try method so it rescues NoMethodErrors and TypeErrors as well as returning nil when called on a nil value.
Like the #try method, #try_rescue takes 1 or more arguments. The first argument is the method to be called on the calling object, passed as a symbol. The others are zero or more arguments that will be passed through to that method, and &block is an optional block that will be similarly passed through.
Example of usage identical to #try:
nil.try(:map) {|a| a.to_s } # => nil
nil.try_rescue(:map) {|a| a.to_s } # => nil
Example of usage calling a method that is not defined on the calling object:
10.try(:to_h) # => TypeError
10.try_rescue(:to_h) # => nil
Example of usage with invalid arguments:
%w(foo, bar, baz).try(:join, [:hello, :world]) # => TypeError
%w(foo, bar, baz).try_rescue(:join, [:hello, :world]) # => nil
34 35 36 |
# File 'lib/extensions/reactive_extensions.rb', line 34 def try_rescue(*args, &block) self.try(*args, &block) rescue nil end |