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
Integer, a String and a Float: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
array.map { |element| element * 2 } puts element
end=> 0 2 4 6 8
.uniq
array = [1,2,3,4,5,5,32,1,7,0] array.uniq[1,2,3,4,5,32,7,0]
.concat
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]

Comments
Post a Comment