Issue #4633 has been updated by mame (Yusuke Endoh).

Description updated
Status changed from Open to Assigned
Assignee set to tarui (Masaya Tarui)


----------------------------------------
Feature #4633: iterate method / extended version of for
https://bugs.ruby-lang.org/issues/4633#change-25122

Author: rbjl (Jan Lelis)
Status: Assigned
Priority: Normal
Assignee: tarui (Masaya Tarui)
Category: 
Target version: 


=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



-- 
http://bugs.ruby-lang.org/