Feature #4633
Updated by mame (Yusuke Endoh) almost 13 years ago
=begin The Ruby world is known for using each, but it does not always look nice (although in most cases it does). I am proposing an iterate method that is nicely readable and allows easy iteration over multiple objects. It behaves like each for an single argument, but passes nils for Enumerables with multiple sizes: iterate [1,2], [3,4,5] do |e,f| puts "#{e},#{f}" end # outputs # 1,3 # 2,4 # ,5 A simple Ruby implementation: def iterate(*params) # params.shift.zip(*params).each{ |*elements| yield *elements } raise ArgumentError, "wrong number of arguments (0)" if params.empty? first = params.shift if params.empty? # single param - like each if block_given? first.map{|e| yield e } else first.map.to_enum end else # multiple params max_size = [first, *params].max_by(&:count).size padded_first = first.to_a + [nil]*(max_size - first.count) # append nils obj = padded_first.zip *params if block_given? obj.map{|es| yield *es } else obj.map.to_enum end end end A modified version of this request (no new method/statement) could be an alternative usage of for, something like: for e,f in [1,2], [3,4,5] puts "#{e},#{f}" end # outputs # 1,3 # 2,4 # ,5 This feature request does not add something needed, but I think, Ruby would look even more beautiful. =end