Ref. WGR Section 6.3, "Iterators and code blocks"
times
is on your sidepotatoes = nil
3.times do |i|
potatoes = i+1
puts "#{potatoes} potato"
end
puts potatoes + 1
prints
1 potato
2 potato
3 potato
4
times
using until
def times x
i = 0
until i == x
yield i
i += 1
end
x
end
(times
returns the number itself)
each
his own ["apple", "banana", "cherry"].each do |fruit|
puts "I love #{fruit}!"
end
def count_chars a
c = 0
a.each do |s|
c += s.length # this returns c
end # but this returns a
c # so this returns c (again)
end
count_chars ["apple", "banana", "cherry"]
=> 17
each
using until
def each a
i = 0
until i == a.size
yield a[i]
i += 1
end
a
end
map
is not the territory["apple", "banana", "cherry"].map do |fruit|
fruit.reverse
end
=> ["elppa", "ananab", "yrrehc"]
map
using each
def map input
a = []
input.each do |item|
a << yield(item)
end
a
end
select
(alias find_all
)
reject
collect
(alias map
)
detect
(alias find
)
inject
Ref. Using Select Etc.
inject
ioninject
is a really fun iterator, but it's really weirdthe return value of the block becomes the next accumulator
"inject" is also called "reduce", "fold", and "accumulate" in other languages
inject
example class Array
def sum
self.inject(0) do |total, current|
total + current
end
end
end
[1,2,3].sum #=> 6
inject
reducedYou can also send inject
(or reduce
) the name of a method only:
class Array
def sum
self.reduce(:+)
end
end
[1,2,3].sum #=> 6
Here we are "reducing" the array by calling +
on all its elements in succession.
/