Skip to content
Justice Tech School

each, map and select

Iterate with each, transform with map and filter with select and reject.

Example
a = [1, 2, 3, 4]
a.each { |x| print x, " " }
puts
p a.map { |x| x * 2 }, a.select(&:even?), a.reject(&:even?)
puts a.sum, a.inject(:*)

Result

1 2 3 4 
[2, 4, 6, 8]
[2, 4]
[1, 3]
10
24

Exercise

Transform items.

a. { |x| x * 2 }

Quiz Test

What does select return?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.