fizz_buzz = ->(i) do
case 0
when i % 15
puts 'FizzBuzz'
when i % 5
puts 'Buzz'
when i % 3
puts 'Fizz'
else
puts i
end
end
(1..40).collect do |i|
i.tap(&fizz_buzz)
end
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
Fizz
22
23
Fizz
Buzz
26
Fizz
28
29
FizzBuzz
31
32
Fizz
34
Buzz
Fizz
37
38
Fizz
Buzz
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40]
Object#tap evaluates block with self as block-argument, and then return self.
No comments:
Post a Comment