Why is try throwing an error? Doesnt that defeat the whole purpose? Maybe its just in the console?
ruby-1.9.2-p180 :101 > User.first.try(:something)
NoMet
This is what try does
Invokes the method identified by the symbol method, passing it any arguments and/or the block specified, just like the regular Ruby Object#send does. 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.
So, let's say you setup @user
in your controller but you didn't instantiate it then @user.try(:foo)
=> nil
instead of
@user.foo
NoMethodError: undefined method `foo' for nil:NilClass
The important point here is that try is an instance method. It also doesn't return nil if the object you try on isn't nil.
You misunderstand how try
works, from the fine manual:
try(*a, &b)
Invokes the method identified by the symbol method, passing it any arguments and/or the block specified, just like the regular RubyObject#send
does.Unlike that method however, a
NoMethodError
exception will not be raised andnil
will be returned instead, if the receiving object is anil
object orNilClass
.
And the version of try that is patched into NilClass:
try(*args)
Callingtry
onnil
always returnsnil
.
So try
doesn't ignore your attempt to call a non-existent method on an object, it ignores your attempt to call a method on nil
and returns nil
instead of raising an exception. The try
method is just an easy way to avoid having to check for nil
at every step in a chain of method calls.
The behavior of try
has changed in Rails 4 so now it:
Invokes the public method whose name goes as first argument just like
public_send
does, except that if the receiver does not respond to it the call returnsnil
rather than raising an exception.
So now try
takes care of both checks at once. If you want the Rails 3 behavior, there is try!:
Same as
try
, but will raise aNoMethodError
exception if the receiving [sic] is notnil
and does not implemented [sic] the tried method.
I know this is old, but it may help somebody else, because this is the first thing that popped up when I Googled this issue. I "borrowed" the code for try and implemented my own try_method method which acts just like try, except that it first checks to see if the method exists before calling send. I implemented this in Object and put it in an initializer, and I can now call it on any object.
class Object
# Invokes the method identified by _method_, passing it any
# arguments specified, just like the regular Ruby <tt>Object#send</tt> does.
#
# *Unlike* that method however, a +NoMethodError+ exception will *not* be raised
# if the method does not exist.
#
# This differs from the regular Ruby <tt>Object#try</tt> method which only
# suppresses the +NoMethodError+ exception if the object is Nil
#
# If try_method is called without a method to call, it will yield any given block with the object.
#
# Please also note that +try_method+ is defined on +Object+, therefore it won't work with
# subclasses of +BasicObject+. For example, using try_method with +SimpleDelegator+ will
# delegate +try_method+ to target instead of calling it on delegator itself.
#
# ==== Examples
#
# Without +try_method+
# @person && @person.respond_to?(:name) && @person.name
# or
# (@person && @person.respond_to?(:name)) ? @person.name : nil
#
# With +try_method+
# @person.try_method(:name)
#
# +try_method+ also accepts arguments and/or a block, for the method it is trying
# Person.try_method(:find, 1)
# @people.try_method(:collect) {|p| p.name}
#
# Without a method argument try_method will yield to the block unless the receiver is nil.
# @person.try_method { |p| "#{p.first_name} #{p.last_name}" }
#--
# +try_method+ behaves like +Object#send+, unless called on +NilClass+ or a class that does not implement _method_.
def try_method(method=nil, *args, &block)
if method == nil && block_given?
yield self
elsif respond_to?(method)
__send__(method, *args, &block)
else
nil
end
end
end
class NilClass
# Calling +try_method+ on +nil+ always returns +nil+.
# It becomes specially helpful when navigating through associations that may return +nil+.
#
# === Examples
#
# nil.try_method(:name) # => nil
#
# Without +try_method+
# @person && @person.respond_to(:children) && !@person.children.blank? && @person.children.respond_to(:first) && @person.children.first.respond_to(:name) && @person.children.first.name
#
# With +try_method+
# @person.try_method(:children).try_method(:first).try_method(:name)
def try_method(*args)
nil
end
end