From: Jan Lelis Date: 2011-04-29T22:01:10+09:00 Subject: [ruby-core:35947] [Ruby 1.9 - Feature #4633][Open] iterate method / extended version of for Issue #4633 has been reported by Jan Lelis. ---------------------------------------- Feature #4633: iterate method / extended version of for http://redmine.ruby-lang.org/issues/4633 Author: Jan Lelis Status: Open Priority: Normal Assignee: Category: Target version: 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. -- http://redmine.ruby-lang.org