[ruby,numbered]
source~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# Useful Ruby base class extensions.
#
class Array
# Execute a block passing it corresponding items in
# +self+ and +other_array+.
# If self has less items than other_array it is repeated.
def cycle(other_array) # :yields: item, other_item
other_array.each_with_index do |item, index|
yield(self[index % self.length], item)
end
end
end
if $0 == __FILE__
# Array#cycle test
# true => 0
# false => 1
# true => 2
# false => 3
# true => 4
puts 'Array#cycle test'
[true, false].cycle([0, 1, 2, 3, 4]) do |a, b|
puts "#{a.inspect} => #{b.inspect}"
end
end
source~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~