[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Redefe each to include each_with_index and arity=2

Dan Diebolt

9/10/2008 5:53:00 PM

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]
8 Answers

Anton Ivanov

9/10/2008 6:46:00 PM

0

Dan Diebolt wrote:
> 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:

> ======
> 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]
>  

I'm not exactly sure why your code does not work. It seems that
each_with_index and each depend on each other in ways which you are not
capturing with your redefined each. But you can roll your own index
tracking:

def each(&block)
i = 0
arity = block.arity
old_each do |curr|
case arity
when 1: yield curr
else yield curr, i
end
i+=1
end
end

--
Posted via http://www.ruby-....

Robert Dober

9/12/2008 1:18:00 PM

0

On Wed, Sep 10, 2008 at 7:53 PM, DanDiebolt.exe <dandiebolt@yahoo.com> wrot=
e:
> arity=3D2
> arity=3D-1

Where do you think the arity=3D-1 does come from? Does this ring a bell?

Actually each_with_index will use a block with *args calling each again.
Now that brings us to why your idea might not be as good as you
thaught at first sight:

[[:a, 42], [:b,43]].each{ |a,b| puts "#{a} =3D> #{b}" }

you see the arity of the block of each is indeed something which might
vary with the usecase.

HTH
Robert
--=20
C'est v=E9ritablement utile puisque c'est joli.

Antoine de Saint Exup=E9ry

Obama Bin Biden's Dead ; Thanks Bush !

5/2/2011 9:31:00 PM

0



"Ron" <roneal1554@att.net> wrote in message
news:238d4030-1c29-417b-9fe3-4004e2f20096@d1g2000yqm.googlegroups.com...

I won't make Bin Laden a martyr, vows Obama as he blasts Bush for failing
to
catch terror leader By Daily Mail Reporter
Last updated at 10:32 AM on 19th June 2008


Mr Obama said of Bin Laden: 'What would be important would be for us to do
it in a way that allows the entire world to understand the murderous acts
that he's engaged in and not to make him into a martyr, and to assure that
the United States government is abiding by basic conventions that would
strengthen our hand in the broader battle against terrorism.'
The senator cited the Nuremberg trials as an inspiration because the
liberators of Nazi-occupied Europe acted to advance universal principles and
set a tone for the creation of an international order.


Read more:
http://www.dailymail.co.uk/news/worldnews/article-1027617/I-wont-make-Bin-Laden-martyr-vows-Obama-blasts-Bush-failing-catch-terror-leader.html#ixz...





Lamont Cranston

5/2/2011 11:16:00 PM

0

On 5/2/2011 2:30 PM, Obama Bin Biden's Dead ; Thanks Bush ! wrote:
>
>
> "Ron" <roneal1554@att.net> wrote in message
> news:238d4030-1c29-417b-9fe3-4004e2f20096@d1g2000yqm.googlegroups.com...
>
> I won't make Bin Laden a martyr, vows Obama

....and he didn't.

spameraser

5/3/2011 1:22:00 AM

0


"Lamont Cranston" <Lamont.Cranston@FogYourMind.com> wrote in message
news:ipndvc$r4b$5@news.datemas.de...
> On 5/2/2011 2:30 PM, Obama Bin Biden's Dead ; Thanks Bush ! wrote:


spameraser

5/3/2011 1:22:00 AM

0


"Obama Bin Biden's Dead ; Thanks Bush !" <Bush's CIA tracking Obama Bin Biden @islamabad.com>
wrote in message news:EnFvp.16630$uh5.9152@newsfe02.iad...
>
>
> "Ron" <roneal1554@att.net> wrote in message
> news:238d4030-1c29-417b-9fe3-4004e2f20096@d1g2000yqm.googlegroups.com...


Eddie Haskell

5/3/2011 2:34:00 PM

0


"Ron" <roneal1554@att.net> wrote in message
news:238d4030-1c29-417b-9fe3-4004e2f20096@d1g2000yqm.googlegroups.com...
On May 2, 10:17 am, Clyde Armstrong <clayno...@gmail.com> wrote:
> The U.S. military was a woeful, weak demoralized institution when
> Reagan took office in 1981. The Pentagon's
> budget had been cut precariously by Jimmy Carter and the Democrat
> Congress after Vietnam.
>
> It was Reagan who reorganized and strengthened the special forces and
> the navy Searls in the 1980s. His successful raid and intervention in
> Grenada was a morale builderas he undertook a trillion dollar
> rebuilding of the entire U.S. military. However the suicide bombing
> in Lebanon was a setback; America's intervention there was a bad
> mistake. But George Bush 41's incursion into Panama was another morale
> booster for th U.S. military
> as the dictator Noriega, a leftist anarchist icon, was embarrassingly
> defeated and put on trial, and is still in prison.
>
> But once again the Democrats returned to power in Congress with the
> Clinton administration and U.S. defence
> was put on the back-burner as the Pentagon and CIA again were
> emasculated by budget cuts. When George Bush
> 43 came to power the U.S. had lost its concern and vigilance for
> military securty even after several Muslim terrorist
> attacks during Clinton's 8- year term.
>
> But after 9/11 Bush and America reawakened and again increased the
> Pentagon's budget and were determined to fight back against the Muslim
> attacks against much Democrat and liberal criticism. America got tough
> with the opening of Guantamo, the use of forceful interrogation,
> rendition etc. The use of the Drone surveillance and attack aircraft
> added to its military potency.
>
> The Democrat Obama has been forced to continue America's belligerence
> against enemies around the world. Thus the Bush government must be
> given much credit for Osama bin Laden's summary assasination--men
> like Dick Cheney, Donald Rumsfeld, General Franks, etc. They are no
> longer in power but there legacy has been successful to the U.S. and
> capitalist world. And America, if not Obama, has said tot the world
> and the UN, fuck
> you and your international "law" and courts.
.
.
> Re: Thus the Bush government must be given much credit for Osama bin
> Laden's summary assasination--men like Dick Cheney, Donald Rumsfeld,
> General Franks, etc.

> Much credit? MUCH?

> "I don't know where bin Laden is. I have no idea and really don't
> care. It's not that important. It's not our priority."
> GWB, March 13, 2002

Reduced to desperate out of context quotes I see.

-Eddie Haskell




Yoorghis

5/3/2011 5:28:00 PM

0

On Tue, 3 May 2011 09:34:06 -0500, "Eddie Haskell" <kimnnn@sawqo.com>
wrote:

>> "I don't know where bin Laden is. I have no idea and really don't
>> care. It's not that important. It's not our priority."
>> GWB, March 13, 2002
>
>Reduced to desperate out of context quotes I see.

But the historical record shows it true.





>=============================================================

On Fri, 18 Sep 2009 16:32:34 -0700 (PDT), Kurtis T. Nicklas of
1293 Westbrook Ave, Elon, NC 27244-9372"

<nickl...@bellsouth.net> wrote in message


>I don't pay much attention to him these days, but I'd wager he's not
>happy.

You sure as shit paid attention when you got caught
making all those late-night hang-up phone calls, didn't
ya, Nickkkkers?

CLICK ! ! !