googleads
10 Ruby Tricks We Must Use To Improve The Code
preloder
Ruby Ruby on Rails

10 Ruby Tricks We Should Use To Improve The Code

In our previous blog, we have clearly explained about the 10 must use ruby on rails tips and tricks that developers must follow. Based on the significant interests and reach of that blog here we are revealing the another set of Ruby tricks and standard practices that will help you to improve the code and also the performance of your application. Along with the tricks, sharing with you the do ‘s and dont’s of Ruby in brief and hoping that it could make your Ruby application to perform better.

1) Use Fetch To Find Value From The Hash

Using hash[] method we can return the value. It will return the value if the key exists, else it will return nil.
Using hash fetch method will also do the same but additionally it will give few options like assigning a default value, executing block, etc.
Here are some examples

Don’ts

 

Do’s

 

Don’ts

 

OR other

 

Do’s

 

2) Mass Variable Assignments

Mass assignment is the useful feature to assign multiple variables in single place. We can use it to return the multiple value from the method and this returned value can be assigned by Mass variable assignments.

Don’ts

 

Do’s

 

3) Use .map Method To Iterate And To Return Modified Object

Developers have to understand the difference between .map and .each methods. When using these functions try to choose the appropriate method based on the situations.
For example, If you want to return the vowels array in the capital letter then use .map method,

Don’ts

 

Do’s

 

Related: 10 Useful Command Line Tools For Developers

 

4) Try To Use “Ternary” Operator

Many developers will use “if else” statement for single line functions, For example,

 

Why we aren’t start using a ternary here?

 

Another way to use the conditions as the return object.

 

5) Use Rescue Blocks Properly

Rescue blocks always not required to use with ‘begin.’ If you are going to use only begin and rescue block in the method, then you can skip the begin.

Don’ts

 

Do’s

 

6) Use .tap To Perform Operations And To Return The Object

Ruby .tap method is used to yields object to the block and will return the modified object. For example,

Don’ts

 

Do’s

This method has the temporary variable to form and return account object. We can change the above method just by using .tap method.

 

7) How To Merge Strings?

Use interpolation to merge strings instead of using concatenation, and Comparatively Interpolation would be faster than the concatenation.

Don’ts

puts “my name is ” + concatenation

Do’s

put “my name is #{interpolation}”

Interpolation supports “type casting” but concatenation won’t support.

8) Retrieving The Nested Hash Value

Use hash dig method to get the nested hash values. Sometimes you have to navigate to multi-level hash to get values. For example, consider the below user hash.

Don’ts

 

Do’s

Ruby provides some special tricks to handle this situation.

 

Related: A Guide for Web Automation Testing With Ruby Cucumber And Watir

 

9) Using & To Create Procs

In many cases developers will use the map method with the block for doing simple operations, instead, they can use “& operator” for better code.

Don’ts

 

Do’s

 

10) Use Bang! Methods

You can avoid the duplicate objects by using bang methods,

Don’ts

 

Do’s

 

Hence we can avoid the redundancies or reduce creating multiple objects by using the bang methods.

The end

Hopefully, trying these Ruby tricks will make a convenient work phase for you. So try it out and share it with your friends so they can also learn. If you also know some helpful Ruby tricks then do share with us in the comment section so that everyone around the Ruby community can learn together. Similarly find more informative blogs on our largest blog repository, 

Stay updated with latest topics & tricks and don’t forget to subscribe us to get the latest updates from diverse technologies. Besides all, Post us your valuable thoughts in the comment section.

For any queries reach us via info@agiratech.com.

The following two tabs change content below.

Saravana

An enthusiastic Tech Lead with 7 plus years of experience in Web development arena. Owns legitimate experience in Ruby, Ruby On Rails, AngularJs, DevOps. Golang, Another add on, This young tech freak never miss a chance to get his hands on planting and Gardening even in his busy weekends.

Leave a Reply

Your email address will not be published. Required fields are marked *

[contact-form-7 id="120788" title="Web Page Form"]

Schedule Your Call