Array Methods in ruby

 

Arrays

What is an Array?

An array is an ordered list of elements that can be of any type. You can define an array by placing a list of elements between brackets like so:

Most Common Ruby Array Methods

 You Should Know



Common Array Methods

A new array can be created by using the literal constructor []. Arrays can contain different types of objects. For example, the array below contains an Integer, a String and a Float:


 Our array will be as follows:

    array = [1,2,3,4,5] 

.length

The .length method is used to the number of elements in your array and returns the count:

array.length   #=>array -> [1,2,3,4,5]
=>5

.first

The .first method accesses the first element of the given array and the element start at index of  0:

array.first   #=>array -> [1,2,3,4,5]
=>1

.last

The .last method accesses the last element of the given array:

array.last   #=>array -> [1,2,3,4,5]
=>5

.take

The .take method returns the first n elements of the array:

array.take(3)   #=>array -> [1,2,3,4,5]
=>[1,2,3]

.drop

The .drop method drop(remove) the elements after given n elements of the array:

array.drop(3)   #=>array -> [1,2,3,4,5]
=>[4,5]

array index

You can access  element in an array by accessing its index. If the index does not exist in the array, so it will be return-> nil 

array[2]   #=>array -> [1,2,3,4,5]
=>3 array[6] =>nil

.pop

The .pop method is permanently remove the last element of an given array:

array.pop    #=>array -> [1,2,3,4,5] in this array 5 will be permanently remove
=>[1,2,3,4]  

.shift

The .shift method will permanently remove the first element of an given array and return this element:

array.shift    #=>array -> [1,2,3,4,5] in this array 1 will be permanently remove
=>[2,3,4,5]  

.push

The .push method will  add an element to the end of an array:    

array.push(66)   #=>array -> [1,2,3,4,5] in this array 66 will be add in the last position
=>[1,2,3,4,5,66]  

.unshift

The .unshift method will allow you to add an element to the start of an array:

array.unshift(12)    #=>array -> [1,2,3,4,5] in this array 12 will be add first position
=>[12,1,2,3,4,5]  

 

.delete

The .delete method is permanently delete given n element of an array:

array.delete(12)    #=>array -> [12,1,2,3,4,5] in this array 12 will be delete 
=>[1,2,3,4,5]  

.delete_at

The .delete_at method allows you to permanently remove an array on specified index:

array.delete_at(1)    #=>array -> [1,2,3,4,5] in this array  will be remove given index
=>[1,3,4,5]  

.reverse

The .reverse method reverses the array and its immutable (the original array stays as is):

array.reverse(1)    #=>array -> [1,2,3,4,5] 
=>[5,4,3,2,1]  

.select

.select() is a Array class method which returns a new array containing all element of array for which the given block returns a true value:

array = [1,2,3,4,5,6,7,8,9,10]  
array.select { |number| number > 4 } 
=> [5,6,7,8,9,10]

.include?

The include? method checks to see if the argument given is present in given the array:

array = [1,2,3,4,5]
array.include?(4)
 => true

.join

The .join method returns a string of all the elements of the array separated by a separator parameter. If the separator parameter is nil, the method uses an empty string as a separator between strings.

array = [1,2,3,4,5]
array.join("*") 
"1*2*3*4*5"

.each

The .each method iterates over each element of the array, allowing you to perform actions on them.

array = [1,2,3,4,5]
array.each do |element|
put element
end
=>
0
1
2
3
4



.map

 map() is a Array class method which returns a new array containing the values returned by the block.
array.map { |element| element * 2 }
  puts element

end

=> 0 2 4 6 8

.uniq

uniq() is a Array class method which returns a new array by removing duplicate values in the array.
array = [1,2,3,4,5,5,32,1,7,0]
array.uniq
[1,2,3,4,5,32,7,0]

.concat

concat() is a Array class method which returns the array after appending the two arrays together.

array = [1,2,3,4,5]
 array.concat([6,7,8,9],[21,34,6])
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 21, 34, 6] 

it this article was helpful, so follow my YouTube>https://www.youtube.com/@codingmaster4132

Comments

Popular posts from this blog

Rails Create new Project

what is devise ? and how to use devise gem

Ruby Variable