try public

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 returns nil rather than raising an exception.

This method is defined to be able to write

@person.try(:name)

instead of

@person.name if @person

try calls can be chained:

@person.try(:spouse).try(:name)

instead of

@person.spouse.name if @person && @person.spouse

try will also return nil if the receiver does not respond to the method:

@person.try(:non_existing_method) # => nil

instead of

@person.non_existing_method if @person.respond_to?(:non_existing_method) # => nil

try returns nil when called on nil regardless of whether it responds to the method:

nil.try(:to_i) # => nil, rather than 0

Arguments and blocks are forwarded to the method if invoked:

@posts.try(:each_slice, 2) do |a, b|
  ...
end

The number of arguments in the signature must match. If the object responds to the method the call is attempted and ArgumentError is still raised in case of argument mismatch.

If try is called without arguments it yields the receiver to a given block unless it is nil:

@person.try do |p|
  ...
end

You can also call try with a block without accepting an argument, and the block will be instance_eval’ed instead:

@person.try { upcase.truncate(50) }

Please also note that try is defined on Object. Therefore, it won’t work with instances of classes that do not have Object among their ancestors, like direct subclasses of BasicObject.

Show source
Register or log in to add new notes.
January 6, 2010
6 thanks

Doesn't return nil if the object you try from isn't nil.

Note that this doesn’t prevent a NoMethodError if you attempt to call a method that doesn’t exist on a valid object.

a = Article.new

a.try(:author) #=> #<Author ...>

nil.try(:doesnt_exist) #=> nil

a.try(:doesnt_exist) #=> NoMethodError: undefined method `doesnt_exist' for #<Article:0x106c7d5d8>

This is on Ruby 1.8.7 patchlevel 174

August 21, 2014 - (v4.0.2)
3 thanks

In Rails 4 it DOES return nil, even if the object you try from isn't nil

The try method does not raise a NoMethodError if you call a method that doesn’t exist on an object.

a = Article.new

a.try(:author) #=> #<Author ...>

nil.try(:doesnt_exist) #=> nil

a.try(:doesnt_exist) #=> nil

Note:

a.try(&:doesnt_exist) #=> Raises NoMethodError
July 24, 2012
3 thanks

rest of code is in NilClass#try

If you click “Show source” here, you may get confused. The logic for #try is shared between this method and NilClass#try . Both versions are currently implemented in the file activesupport/lib/active_support/core_ext/object/try.rb .

June 10, 2009
2 thanks

[:a, :b, :c].try([1]) ? The answer is No.

Correct way is this:

[:a, :b, :c].try(:at, 1)
September 13, 2013
2 thanks

Edge case

NilClass#try doesn’t check for methods on itself:

nil.blank?        #=> true
nil.try :blank?   #=> nil
January 6, 2010
1 thank

Alternative:

without using at

[:a, :b, :c].try(:[], 1)
August 10, 2017 - (v2.2.1 - v4.2.7)
0 thanks

PAGER DUTY & EXCEPTION NOTIFIER PLUGINS FOR RAILS

RAILS EXCEPTION NOTIFIER

The Exception Notifier plugin provides a mailer object and a default set of templates for sending email notifications when errors occur in a Rails application. It is basically a monitoring tool, which keeps on watching the application and whenever it finds any error, it triggers that error to PagerDuty. To use Exception Notification and PagerDuty in your app, you need to add this gem below:

gem 'exception_notification', '~> 4.1.0'

gem  'pagerduty''

To get the email notifications, you need to include the line below in the development env:

Rails.application.config.middleware.use  ExceptionNotification::Rack,
                 :email => {
                  :email_prefix => "[PREFIX] ",
                  :sender_address => %{"notifier"  <notifier@example.com>},
                  :exception_recipients => %w{exceptions@example.com},
                  :pd => {
                           # simple notifier options
                         }
               }

You can modify sender’s and recipient’s address.

Rails App+PagerDuty

Use the code below in your app with exception notifier to connect with PagerDuty:

require "pagerduty"
module ExceptionNotifier
      Class PdNotifier
          def initialize(options)
            @pagerduty =   Pagerduty.new("0bdcfdacf1b144d7822dfdfa5ed0ab1e")# Service api key
           # do something with the options...
         end
        def call(exception, options={})
           @pagerduty.trigger(exception.message, details:  { backtrace: exception.backtrace })
        end
    end
end

Conclusion

PagerDuty is alert dispatching tool used by operations team/OnCall Engineers to manage the applications and it is popular because of its reliable & rich services(Scheduling,Alerting,Reporting,Call Routing , Feedback & response time).

Create your Free account from app.pagerduty.com/ and integrate with your application to get the flow , how Incident is triggered.

February 3, 2015
0 thanks

Poor man's maybe

After creating a simple Maybe monad in Ruby, a colleaque noticed I could have just used try (I wasn’t aware try supports blocks). I think the method was even meant for such cases.

Why I mention this? Because it clarifies the whole ‘raises exception if method does not exist’ thing. It should not be crappy solution to exception handling, but allow for doing away with messy if statements. An example:

report = params[:query_type]
.try { |qt| build_query(qt) }
.try { |sql| run_query(sql) }
.try { |res| format_result(res) }

If any of the expressions params[], build_query, run_query etc. returns nil, the chain is halted and nil is returned. It still throws exceptions if a values is not nil and method does not exist, which is just like it should.

June 28, 2017
0 thanks

How to use Textacular Gem to search data in your Rails Application

We might have heard about a lot many gems which let us implement search functionality in our rails application; for example: searchkick, elasticsearch-rails, ransack and finally, sunspot to work with solr search engine. All these gems have their own advantages. Both searchkick and elasticsearch use redis to search the data as well as need to perform a ‘reindex’ while inserting new data. In one of my recent projects, I happened to use a gem called as Textacular. It’s simple and very easy to use. Textacular Gem:

It is a gem that provides full text search capabilities for PostgreSQL Database. It basically caters to extend the scope of the work performed by activerecord, in a rather friendly manner. It works on heroku as well. This gem works only on PostgreSQL For working with it, let’s first grab the latest textacular gem from rubygems.org/gems/textacular and add it to the gemfile.

gem 'textacular'
    bundle install

Textacular gem provides us with quite a few methods to search the data. So, all our models have the access to use those methods.

basic_search advanced_search fuzzy_search

Usage: Basic_search: It searches based on the input text.

User.basic_search(‘abc’) # Searches on all the model column attributes

User.basic_search(last_name: 'abc', first_name: 'xyz')

Advanced_search: Here, we can use postgres syntaxes like !, & and | (and, or and, not) and then, some others based on the requirement. It converts user’s search DSL into Pg syntax. For this, we need to make sure that the necessary exceptions should be used to handle the syntax errors.

User.advanced_search(last_name: 'text1|text2’) - It  searches with the text1 or text2 on last_name on User  model.

User.advanced_search(last_name: '!text2’) - It searches for the records whose last_name is not text2.

These searches can be chainable as shown below:

User.advanced_search(last_name: 'text1|text2’).basic_search(last_name: 'abc', first_name: 'xyz')

Fuzzy_search: We need to install pg_trgm module to work with fuzzy_search. Run the command below to install this module. It searches for partial appearance of your text in the DB.

rake textacular:create_trigram_migration  
rake db:migrate

Now, we are ready to use fuzzy_search.

User.fuzzy_search('Lorem')

By default, fuzzy search, searches for the records which are 30% of the search text matches with respect to the entire content. We can set this threshold limit by using the command below.

ActiveRecord::Base.connection.execute("SELECT set_limit(0.6);")

So, it expects 60% of search text to match with the original content. We can use OR condition to search on multiple columns. Need to pass a hash with columns with input text as param one and pass second param as a false. It takes AND, if you miss second param or if it True.

User.fuzzy_search({first_name: 'user', last_name: 'test'}, false)

User.fuzzy_search(first_name:user’, last_name: 'test') - It takes AND condition.

By default, the Textacular searches in all text and string columns. We can alter its default behaviour by overriding the searchable_columns method in the model.

def self.searchable_columns
   [:title, :body]
end

We can override self.searchable_language in the model to set proper dictionary settings.

def self.searchable_language 
  'arabic' 
end

Read More : http://www.railscarma.com/blog/technical-articles/how-to-use-textacular-gem-to-search-data-in-your-rails-application/