[1, 2, 3].each do |number|
puts "hi #{number}"
end
log:
hi 1
hi 2
hi 3
result = [1, 2, 3, 4].map do |number|
number + 2
result = [3, 4, 5, 6]
result = [1,2,3].flat_map do |number|
['a', 'b', 'c'].map do |letter|
"#{number}:#{letter}"
end
result = ["1:a", "1:b", "1:c", "2:a", "2:b", "2:c", "3:a", "3:b", "3:c"]
result = [1, 2, 3, 4].select do |number|
number.even?
result = [2,4]
result = [1, 2, 3, 4].detect do |number|
result = 2
result = [1, 2, 3, 4].reject do |number|
result = [1,3]
result = [1, 2, 3, 4].partition do |number|
Each
[1, 2, 3].each do |number|
puts "hi #{number}"
end
log:
hi 1
hi 2
hi 3
Map
result = [1, 2, 3, 4].map do |number|
number + 2
end
result = [3, 4, 5, 6]
Flat Map
result = [1,2,3].flat_map do |number|
['a', 'b', 'c'].map do |letter|
"#{number}:#{letter}"
end
end
result = ["1:a", "1:b", "1:c", "2:a", "2:b", "2:c", "3:a", "3:b", "3:c"]
Select
result = [1, 2, 3, 4].select do |number|
number.even?
end
result = [2,4]
Detect
result = [1, 2, 3, 4].detect do |number|
number.even?
end
result = 2
Reject
result = [1, 2, 3, 4].reject do |number|
number.even?
end
result = [1,3]
Partition
result = [1, 2, 3, 4].partition do |number|
number.even?
end