Skip to main content

Frequently Used Method - Array

Array
1. include?
It will return true if the object passed in match the element in the array. It follows this format, population.include?(sample)
eg: a = [1,2,3]
a.include?(2) 
returns true

2. map / map.with_index  aka.  collect
Iterates through the array , then , create a new array with values returns by the block of code.
Eg: to double all integers in an array
array = [1,2,3]
doubled = array.map { |element| element * 2  }
doubled = [2,4,6]

Another short hand method of writing map is this
Eg: to convert an array of strings into integers.
a = ['2', '3'].map(&:to_i)
a = [2,3]
This will return [2, 3]

It is the same as the method below.
['2', '3'].map { |element| element.to_i }

3. [] = 

It will return the value at index position passed in.
Eg :
a = [1,2,3,4]
a[0] = 1
a[-2] = 3

In OOP, if you want to override this method for your object.
Eg:
def []=(key, value)
end


4. each / each_with_index
Iterate through the array. Returns the original array. The variant with_index creates an index block variable.
a = [1,2,3]
a.each do |x|
   puts x
end
Returns [1,2,3] 
Prints 1, 2, 3

5. flatten
break nested array into one big array.
Eg:
a = [[1,2],[3,4]]
a.flatten 
returns [1,2,3,4]

6. count
count the number of element in an array without argument. With arguments, count the occurrences of the arguments in the array.
Eg;
a = [1,1,2,3]
a.count(1) 
# returns  2
a.count
# returns 4
a.count { |x| x < 2 }
# return 2

7. join
Join an array into a string with or without a specified delimiter
Eg:
a = ["it", "is", "a", "good", "day"]
a.join(" ")
returns "it is a good day"

8. first , last

returns the first and last element in the array respectively.
Eg:
a = [1,2,3,4]
a.first    returns 1
a.last     returns 4

9. push , <<

append the arguments into the array. Mutates the caller.
eg:
a = [1,2,3,4]
a.push(5,6)
a = [1,2,3,4,5,6]

10. select

return an array with elements that evaluates to true in the block.
eg:
a = [1,2,3,4,5]
a.select { |x| x>3 }
returns[4,5]

11. size , length
returns the number of elements in an array
eg:
a = [1,2,3,4]
a.size     
returns 4

12. slice
return a portion of the array.
eg:
a = [1,2,3,4,5]
a.slice(1,2)  returns [2,3]
a.slice(2..3) return [3,4]

13. pop , shift
pop removes the last element of the array, return last element.
shift removes the first element of the array, return first element
both mutate the caller.
eg:
a = [1,2,3,4,5]
a.pop     return 5
a = [1,2,3,4]
a.shift    return 1
a = [2,3,4] 

14. uniq
return a new array with duplicate values removed.
eg:
a = [1,2,3,4,5,5,5]
a.uniq    return [1,2,3,4,5]

15. sample
return a random element in the array
eg:
a = ['a' , 'b', 'c'] 
a.sample    return 'b'  
a.sample    return 'a'

16. sort
return a new array with the element sorted from smallest to largest.
eg:
a= [1,5,2,1,3]
a.sort    return [1,1,2,3,5]

17. reduce , inject
perform the stated operation for all elements in the array
eg :
a = [1,2]
a.reduce(:+)  # return 3
a.inject(:*)    # return 2




Comments