What's new in Ruby 2.5?

2017-10-12 09:27:58 +0000

Ruby 2.5.0-preview1 Released

useful image

We are pleased to announce the release of Ruby 2.5.0-preview1.

Ruby 2.5.0-preview1 is the first preview release toward Ruby 2.5.0. It introduces some new features and performance improvements, for example:

After installing the beta version through rvm …

 tinix   rvm install 2.5.0-preview1
Searching for binary rubies, this might take some time.
No binary rubies available for: debian/9/x86_64/ruby-2.5.0-preview1.
Continuing with compilation. Please read 'rvm help mount' to get more information on binary rubies.
Checking requirements for debian.
Requirements installation successful.
Installing Ruby from source to: /home/tinix/.rvm/rubies/ruby-2.5.0-preview1, this may take a while depending on your cpu(s)...
ruby-2.5.0-preview1 - #downloading ruby-2.5.0-preview1, this may take a while depending on your connection...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 13.4M  100 13.4M    0     0  1693k      0  0:00:08  0:00:08 --:--:-- 2192k
No checksum for downloaded archive, recording checksum in user configuration.
ruby-2.5.0-preview1 - #extracting ruby-2.5.0-preview1 to /home/tinix/.rvm/src/ruby-2.5.0-preview1....
ruby-2.5.0-preview1 - #configuring.............................................|
ruby-2.5.0-preview1 - #post-configuration..
ruby-2.5.0-preview1 - #compiling.........................................................................|
ruby-2.5.0-preview1 - #installing..............
ruby-2.5.0-preview1 - #making binaries executable..
Rubygems 2.6.14 already available in installed ruby, skipping installation, use --force to reinstall.
ruby-2.5.0-preview1 - #gemset created /home/tinix/.rvm/gems/ruby-2.5.0-preview1@global
ruby-2.5.0-preview1 - #importing gemset /home/tinix/.rvm/gemsets/global.gems.............................|
ruby-2.5.0-preview1 - #generating global wrappers........
ruby-2.5.0-preview1 - #gemset created /home/tinix/.rvm/gems/ruby-2.5.0-preview1
ruby-2.5.0-preview1 - #importing gemsetfile /home/tinix/.rvm/gemsets/default.gems evaluated to empty gem list
ruby-2.5.0-preview1 - #generating default wrappers........
ruby-2.5.0-preview1 - #adjusting #shebangs for (gem irb erb ri rdoc testrb rake).
Install of ruby-2.5.0-preview1 - #complete
Ruby was built without documentation, to build it run: rvm docs generate-ri
 tinix  
 

The 2.5.0-preview1Ruby language version has just been released. Because it is not a stable version, its use is not yet recommended .

** Inverse backtrace**

The backtrace (list of methods that were run before an error happened) will start to be displayed in reverse order. Using the following code as an example:

#file test.rb

def a
   b
end

def b
   c
end

def c
   raise 'error'
end

a

if the same file is run in ruby 2.4 you get…

$ ruby test.rb
test.rb:10:in `c': error (RuntimeError)
    from test.rb:6:in `b'
    from test.rb:2:in `a'
    from test.rb:13:in `<main>'
	Rescue / else / ensure will be allowed inside of / end blocks

Running with Ruby 2.4:

Currently we can only capture exceptions within methods or blocks begin/ end, ie:

begin
  raise 'boom'
rescue Exception => e
  puts "Error caught: #{e.message}"
end

: Output Error caught: boom

def my_method
  raise boom
  rescue Exception => and
  puts "Error caught: # {e.message}"
end

: Output Error caught: boom.

But the code below doesn’t work:

[1, 2, 3].map do |i|
  raise 'boom'
  rescue Exception => e
  puts "Error caught: #{e.message}"
end
syntax error, unexpected keyword_rescue, expecting keyword_end
rescue Exception => e

Running with Ruby 2.5:

In the version that is about to be released, this is possible. The output of the above code will be:

Captured error: boom
Captured error: boom
Captured error: boom

New method yield_self

According to the official documentation Ruby 2.5

  • Yields self to the block and returns the result of the block.

Although very similar to the tapclass method Object(which is very useful, by the way), your returns are different.

While with the method tapyou open a block and at the end the object itself is returned, with the yield_self, the result of the block is returned.

Example:

object.tap {| obj | obj.save} # => object

The save method was called, but object is returned

object.yield_self {| obj | obj.save} # => true
#The last block execution will be returned.
#That is, assuming that the `save` method returns` true`,
#the return of this entire line will be `true` too

I could not think of a practical case to use it, but certainly had a reason to have been added :)

Other changes

The new version will also bring other new features, such as support for Unicode version 10, which includes 56 emojis created this year . You can see a complete list with all the news in this document .

To install Ruby 2.5 (though not yet recommended for not being a stable release) through RVM, simply run:

rvm install 2.5.0-preview1

for more information visit Ruby 2.5.0-preview1 Released

Yes!!! Enjoy Ruby 2.5.0-preview1 Released

redirect_to(options = {}, response_status = {}) protected

2017-10-02 21:25:18 +0000

Redirects the browser to the target specified in options. This parameter can take one of three forms:

  • Hash - The URL will be generated by calling url_for with the options.
  • Record - The URL will be generated by calling url_for with the options, which will reference a named URL for that record.
  • String starting with protocol:// (like http://) - Is passed straight through as the target for redirection.
  • String not containing a protocol - The current protocol and host is prepended to the string.
  • :back - Back to the page that issued the request. Useful for forms that are triggered from multiple places. Short-hand for redirect_to(request.env["HTTP_REFERER"])

Examples:

 redirect_to :action => "show", :id => 5
  redirect_to post
  redirect_to "http://www.rubyonrails.org"
  redirect_to "/images/screenshot.jpg"
  redirect_to articles_url
  redirect_to :back

The redirection happens as a “302 Moved” header unless otherwise specified.

Examples:

redirect_to post_url(@post), :status => :found
  redirect_to :action=>'atom', :status => :moved_permanently
  redirect_to post_url(@post), :status => 301
  redirect_to :action=>'atom', :status => 302

It is also possible to assign a flash message as part of the redirection. There are two special accessors for commonly used the flash names alert and notice as well as a general purpose flash bucket.

Examples:

  redirect_to post_url(@post), :alert => "Watch it, mister!"
  redirect_to post_url(@post), :status=> :found, :notice => "Pay attention to the road"
  redirect_to post_url(@post), :status => 301, :flash => { :updated_post_id => @post.id }
  redirect_to { :action=>'atom' }, :alert => "Something serious happened"

When using redirect_to :back, if there is no referrer, RedirectBackError will be raised. You may specify some fallback behavior for this case by rescuing RedirectBackError.

Using Delayed Jobs in Rails

2017-09-26 09:00:40 +0000

When creating a web application sometimes need arises that we have to schedule some jobs during the execution of a particular method. Due to these job the render of the page has to wait until the task gets over. This waiting can be sometimes annoying because a user who is waiting for a job to get completed will have to wait for a lot of time if the number of jobs to be performed is more. For example if we take the scenario of sending mails to the user. Now for this if we have large number of users to whom we want to send a mail from our application so it will take a lot of time to get completed which no one will like to wait. So by using delayed jobs what we can do is we can execute the processes in the background and user wont have to wait till all the processes are completed. For that we will be using a gem called

gem 'delayed_job_active_record'

place this gem in your gemfile and run bundle install. Now after done with this we need to add the generator of this gem because active record actice records backend requires a job table in which the jobs will be stored in a queue. You can create the table by running the following command

rails generate delayed_job:active_record

and then run rake db:migrate Now when using this in development mode we need to set the queue_adapter in config/application.rb

config.active_job.queue_adapter = :delayed_job

with the help of this queue adapter we dont need to restart delayed Job every time we update our code in development. Now once done with this we will have to start the rake job in our console which will allow the jobs to run in the background without having the need to restart it again and again. We need to write this in console

rake jobs:work

This will start the rake job thread and will wait for a process or a task to come to it. Now last thing which we need to do is call the delay method where we actually need our jobs to be processed in background without affecting the current execution. Suppose oif you are sending a mail to all the users who have subscribed for a magazine so the users wont have to wait until the mail is sent to all the users. we will just add the delay method like this

NotificationMailer.delay.notification_email(email)

Here notification mailer is the name of the mailer and notification_email is the method through which the mail is being sent. Now the mail will be sent to all users in the background and the execution of the method wont be delayed.

Before_validation and after_touch callbacks in Rails

2017-09-26 09:00:40 +0000

Rails provide us with some pre-defined methods or hooks which can be invoked after or before the execution of a certain method in the object life cycle. Callbacks are called in certain moments of object’s life cycle which we will get to know in this blog. Today we will be studying about three callbacks as follows : before_validation As the name suggests, this method is called before validation of all the fields of the form. For example, there is a registration form which has an email field, so as soon as the user will fill the email field and submit the form before the value gets saved into the database we have to make sure that all characters in the email should be down case. So in this case, we will invoke a before_validation callback in which we will define a method to be called before the create method is called. In this method, we will convert the whole string into down case and after that, the create method will be called like this.

class Employee < ApplicationRecord
  
  before_validation :normalize_email, on: :create
 
  # :on takes an array as well
  
  def create
    ...
  end 
  protected
    def normalize_email
      self.email = email.downcase.titleize
    end
end

after_touch Whenever an object is being touched the after_touch callback is called . For example We create an employee object and as soon as we touch the object the after_touch callback is invoked.

class Employee < ApplicationRecord
  
  after_touch do |employee|
    puts "The object has been touched"
  end
end
 
>> e = Employee.create(name: 'Tom')
=> #<Employee id: 1, name: "Tom", created_at: "2013-11-25 12:17:49", updated_at: "2013-11-25 12:17:49">
 
>> e.touch
The object has been touched
=> true

So hope this article was helpful to understand both the callbacks.

Creating a random string on Rails

2017-09-26 00:49:08 +0000

As we know that in almost all web applications we have to ammend functionalities in which we need to create a random string. This string can contain some alpha numeric characters, numbers, lower case alphabets, upper case alphabets and etc. This random string or token can be generated for many purposes like OTP ( one time password), recovery code etc. Today we will see how to generate an eight character token with the help of arrays. This token will have some numbers, upper case alphabets and lower case alphabets in all jumbled up manner. Here is how we can do it…

token = (('0'..'9').to_a + ('a'..'z').to_a + ('A'..'Z').to_a).shuffle.first(8).join

now as we can see we have taken a variable token in which we are storing numbers from 0 to 9 in the second bracket we have taken all the lower case alphabets. in the third bracket we have taken all the upper case alphabets. and converted them all into array through to_a method. the plus sign concatenates all the arrays into one then finally the .shuffle method will jumble up the whole string and first(8) method will take the first 8 characters from that jumbled up string hence giving us the required token. Now everytime we will call this method it will generate a random string like this as seen in the picture below:

2.2.5 :002 > token = (('0'..'9').to_a + ('a'..'z').to_a + ('A'..'Z').to_a).shuffle.first(8).join
 => "a9bIGCwP" 
2.2.5 :003 > token = (('0'..'9').to_a + ('a'..'z').to_a + ('A'..'Z').to_a).shuffle.first(8).join
 => "OZimKuqJ" 
2.2.5 :004 > token = (('0'..'9').to_a + ('a'..'z').to_a + ('A'..'Z').to_a).shuffle.first(8).join
 => "c09NCv8Z" 
2.2.5 :005 > token = (('0'..'9').to_a + ('a'..'z').to_a + ('A'..'Z').to_a).shuffle.first(8).join
 => "SyVPNqso" 
2.2.5 :006 > token = (('0'..'9').to_a + ('a'..'z').to_a + ('A'..'Z').to_a).shuffle.first(8).join
 => "mLhlOeQT" 
2.2.5 :007 > 

So I hope this blog was helpful to create a random string for multi purposes..!!


« Prev 1 2 3 Next »


subscribe via RSS