From: "DanDiebolt.exe" Date: 2008-09-11T02:53:12+09:00 Subject: Redefe each to include each_with_index and arity=2 --0-1249599327-1221069582=:13751 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 ach= eived with an extra parameter to the block each is called with? In other wo= rds, how do you redefine each so that instead of using this construct: =A0 =A0 a.each_with_index do |item,index| =A0 =A0 end =A0 you can use this one: =A0 =A0 a.each do |item,index| =A0 =A0 end =A0 as well not disturbing this usage: =A0 =A0 a.each do |item| =A0 =A0 end =A0 =3D=3D=3D=3D=3D=3D This is my attempt to redefine Array#each but it does not work: =A0 class Array =A0 alias :old_each :each =A0 def each(&block) =A0=A0=A0 puts "arity=3D#{block.arity}" =A0=A0=A0 case block.arity =A0=A0=A0 when 1=20 =A0=A0=A0=A0=A0 old_each &block =A0=A0=A0 when 2 =A0=A0=A0=A0=A0 each_with_index &block =A0=A0=A0 end =A0 end end =A0 a=3D[1,2,3] =A0 irb(main):030:0> a.each do |item| irb(main):031:1*=A0 puts "item=3D#{item}" irb(main):032:1> end arity=3D1 item=3D1 item=3D2 item=3D3 =3D> [1, 2, 3] =A0 irb(main):033:0> a.each do |item,index| irb(main):034:1*=A0 puts "item=3D#{item}, index=3D#{index}" irb(main):035:1> end arity=3D2 arity=3D-1 =3D> [1, 2, 3] --0-1249599327-1221069582=:13751--