With Rails 2.3.5 I’ve written a module in RAILS_ROOT/lib/foo.rb which I’m including in some models via “include Foo” and all is well except where I try to use some_object.try(:some_method) in the module code – it throws a NoMethodError rather than returning nil like it would from a Rails model/controller/etc. Do I need to require a Rails file from my module?
With Rails 2.3.5 I’ve written a module in RAILS_ROOT/lib/foo.rb which I’m including in some
Share
The
trymethod is added by Rails’ ActiveSupport module, so you need torequire active_supportwithin your module.Edit: Alternatively, it’s trivial to add it to
Objectyourself if you don’t want to bring in the whole of ActiveSupport:From active_support/lib/active_support/core_ext/object/try.rb: