Ruby 2.5.0-preview1 Released
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 …
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:
if the same file is run in ruby 2.4 you get…
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:
: Output Error caught: boom
: Output Error caught: boom.
But the code below doesn’t work:
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:
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.
save method was called,
object is returned
I could not think of a practical case to use it, but certainly had a reason to have been added :)
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
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"])
The redirection happens as a “302 Moved” header unless otherwise specified.
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.
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.
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
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
and then run rake db:migrate Now when using this in development mode we need to set the queue_adapter in config/application.rb
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
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
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.
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.
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…
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:
So I hope this blog was helpful to create a random string for multi purposes..!!
subscribe via RSS