try
- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.8 (0)
- 3.0.0 (0)
- 3.0.9 (-2)
- 3.1.0 (17)
- 3.2.1 (0)
- 3.2.8 (17)
- 3.2.13 (0)
- 4.0.2 (9)
- 4.1.8 (0)
- 4.2.1 (38)
- 4.2.7 (0)
- 4.2.9 (0)
- 5.0.0.1 (-9)
- 5.1.7 (0)
- 5.2.3 (0)
- 6.0.0 (0)
- 6.1.3.1 (0)
- 6.1.7.7 (0)
- 7.0.0 (0)
- 7.1.3.2 (0)
- 7.1.3.4 (0)
- What's this?
try(*a, &b)
public
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.
If try is called without a method to call, it will yield any given block with the object.
Please also note that try is defined on Object, therefore it won’t work with subclasses of BasicObject. For example, using try with SimpleDelegator will delegate try to target instead of calling it on delegator itself.
Examples
Without try
@person && @person.name
or
@person ? @person.name : nil
With try
@person.try(:name)
try also accepts arguments and/or a block, for the method it is trying
Person.try(:find, 1) @people.try(:collect) {|p| p.name}
Without a method argument try will yield to the block unless the receiver is nil.
@person.try { |p| "#{p.first_name} #{p.last_name}" }
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
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
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 .
Edge case
NilClass#try doesn’t check for methods on itself:
nil.blank? #=> true nil.try :blank? #=> nil
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.
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.
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