A record of what you learned while studying for the Ruby Engineer Certification Exam.
zip Methods provided by the Enumerable module. The Array class includes the Enumerable module.
a = [1,2,3]
b = [4,5,6]
a.zip(b) #=>[[1, 4], [2, 5], [3, 6]]
[1,2,3].zip([4,5,6])
#=>=> [[1, 4], [2, 5], [3, 6]]
(5..10).to_a #=>[5,6,7,8,9,10]
(5...10).to_a #=>[5,6,7,8,9]
select/find_all Methods provided by the Enumerable module. The Array class includes the Enumerable module. Execute a block for each element, find the element for which the result is true (find / select), and return it as an array.
numbers = [1,2,3,4,5]
numbers.select{|n|.even?} #=> [2, 4]
numbers.find_all{|n|.even?} #=> [2, 4]
Recommended Posts