--0-1249599327-12210695823751
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

Is is possible to redefine each so that each_with_index behavior can be acheived with an extra parameter to the block each is called with? In other words, how do you redefine each so that instead of using this construct:
a.each_with_index do |item,index|
end
you can use this one:
a.each do |item,index|
end
as well not disturbing this usage:
a.each do |item|
end
======
This is my attempt to redefine Array#each but it does not work:
class Array
alias :old_each :each
def each(&block)
  puts "arity=#{block.arity}"
  case block.arity
  when 1 
    old_each &block
  when 2
    each_with_index &block
  end
end
end
a=[1,2,3]
irb(main):030:0> a.each do |item|
irb(main):031:1*puts "item=#{item}"
irb(main):032:1> end
arity=1
item=1
item=2
item=3
=> [1, 2, 3]
irb(main):033:0> a.each do |item,index|
irb(main):034:1*puts "item=#{item}, index=#{index}"
irb(main):035:1> end
arity=2
arity=-1
=> [1, 2, 3]
--0-1249599327-12210695823751--