[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: Enumerable#inject is surprising me...

ts

10/9/2003 8:55:00 AM

>>>>> "d" == dblack <dblack@superlink.net> writes:

d> $ ./ruby -e 'p ["a"].inject("start") {|x,y| break "b"}'
d> "start"

d> I'm still not understanding why these should be different, or how
d> "start" gets into the first one (the first block) at all.

svg% ruby -e 'p ["x", "y"].inject("a") {|x,y| break 12 if y == "y"; x + y}'
"ax"
svg%

#inject ignore the value returned by break, and return the last result.

In your case, because it exit at the first iteration it return the initial
value "start"


Guy Decoux