Christoph
10/9/2003 6:01:00 PM
dblack wrote:
....
> > 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.
>
> I thought "break val" executed a break *and* generated a return value
> for the block at the same time:
>
> irb(main):011:0> a = proc { break 3 } .call; a
> => 3
>
> Why wouldn't #inject treat the break the same way?
Hi,
david assuming that the ``yield break thing'' would not
be in a broken state (relative to my own POLS that is)
you could implement inject along the lines
module Enumerable
def inject(start = nil)
if start
each {|e| start = yield start,e }
else
first = true
each {|e|
unless first
start = yield start,e
else
start = e
first = false
end
}
end
return start
end
end
in other words even if the inject iteration is is governornated
- ups terminated - unexpectedly at the end you would still
return the current ``start'' value.
/Christoph