Nine Array Methods in Ruby Programming and their Differences - You Should Know About!

25 Sep 2018
0
Comments

   

Nowadays, data handling is a big responsibility for every web and mobile application. Arrays make the process easier and it can handle any list of data. Once you have data in an array, you can easily sort it, remove duplicates, change its order to either ascending or descending and many more.

 

In Ruby programming, there are many methods for Array object i.e. Map, Collect, Select, Reject, Each, Inject, Reduce, Detect and Find. Most of the Ruby on Rails developers uses these methods in their web applications.

 

In this blog, we have outlined the difference between all the methods.

 

Let’s have a look at them to choose the one which is best suitable for your requirement.

 

1. Map

 

The map method takes each element from the array and evaluates
given block on it. Output will be in the form of an array containing evaluated results without changing the original array.

 

Array Methods in Ruby

 

 

2. Map!

 

If you want to modify the original array by replacing the output array elements with the original array elements then you can use the map! method.

 

Ruby Methods for Array

 

 

3. Collect

 

There is no difference between collect and map method both methods work same. Similarly map! and collect! methods are also same.

 

 

Array Method in Ruby on Rails

 

 

4. Select

 

The select method takes each element from the array and evaluates given block on it. If block or statement evaluates to true the original elements gets added into the output array. In this case output array and original array are different. If you want to replace the original array to the output array, you can use select! Method.

 

Array Methods in Ruby Programming

 

 

5. Reject

 

The reject method is exactly opposite to the select method. It will take each element of the array in the block to evaluate. The output array will consist of all the elements in the original array for which block evaluates to false. The reject! method is also opposite to the select! method.

 

Array Method in Ruby on Rails Programming

 

6. Each

 

The each method will iterate over array taking each element from it for evaluation. It will return the original array, but not evaluated one. Now, it depends on you what kind of operation you want to perform in the block.

 


How to use Ruby Methods in Arrays

 

 

7. Inject

 

The inject method takes two arguments, one as an accumulator and other as each element in the array. Accumulator accumulates the evaluated values. This returns the final value of the accumulator as a result. The initial value of the accumulator can be defined if not define it takes the first element of the array as initial value.

 

 

Ruby on Rails Programming Developers uses this Array Methods

 

 

8. Reduce

 

Reduce method is similar to the inject method.

 

Reduce Method in Ruby for Arrays

 

 

9. Detect

 

The detect method is similar to the select method, but the only difference is its output. The detect method also evaluates a block for each element of the array, but returns the first element in the array for which the block evaluates to true or statement in the block evaluates to true.

 

Ruby on Rails Programming with Arrays

 

 

10. Find

 

Find method is similar to Detect method.

 

Ruby on Rails Development and Array Methods

 

  

 




Leave a comment:

Recent Blogs
Contact-Us Contact-Us
Newsletter Newsletter