Ask a Question related to Ruby, Design and Development.
-
dblack@superlink.net #1
Re: Enumerable#inject is surprising me...<Pine.LNX.4.44.0310090713420.21222-100000@ool-4355dfae.dyn.optonline.net>
Hi --
On Thu, 9 Oct 2003, ts wrote:
I thought "break val" executed a break *and* generated a return value>> >>>>> "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.
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?
David
--
David Alan Black
home: [email]dblack@superlink.net[/email]
work: [email]blackdav@shu.edu[/email]
Web: [url]http://pirate.shu.edu/~blackdav[/url]
dblack@superlink.net Guest
-
case where regex range should raise<Pine.LNX.4.44.0310090707440.21222-100000@ool-4355dfae.dyn.optonline.net>
Hi -- On Thu, 9 Oct 2003, Robert Klemme wrote: This is also similar to the case Guy showed: irb(main):004:0> /a{-1,2}/.match("a{-1,2}")... -
Enumerable#inject is surprising me...<Pine.LNX.4.44.0310082132090.19888-100000@ool-4355dfae.dyn.optonline.net>
Hi -- On Tue, 7 Oct 2003, Yukihiro Matsumoto wrote: Using the new version: $ ./ruby -v ruby 1.8.0 (2003-10-06) $ ./ruby -e 'p... -
Enumerable#inject is surprising me...<Pine.LNX.4.44.0310082210440.20124-100000@ool-4355dfae.dyn.optonline.net>
Hi -- On Thu, 9 Oct 2003, Nathaniel Talbott wrote: Hmmm... something changed that day in the #inject + break realm, though maybe it wasn't... -
Enumerable#inject is surprising me...<Pine.LNX.4.44.0310082145050.20002-100000@ool-4355dfae.dyn.optonline.net><Pine.LNX.4.44.0310082132090.19888-100000@ool-4355dfae.dyn.optonline.net>
On Thu, 9 Oct 2003 dblack@superlink.net wrote: Whoops, I mean how it gets into the second one at all. David -- David Alan Black -
Enumerable#inject is surprising me...<Pine.LNX.4.44.0310062142170.7305-100000@ool-4355dfae.dyn.optonline.net>
Hi -- On Tue, 7 Oct 2003 nobu.nokada@softhome.net wrote: I'm not sure why it would be "". Wouldn't that only happen if the empty string... -
Christoph #2
Re: Enumerable#inject is surprising me... <Pine.LNX.4.44.0310090713420.21222-100000@ool-4355dfae.dyn.optonline.net>
dblack wrote:
....y}'> > svg% ruby -e 'p ["x", "y"].inject("a") {|x,y| break 12 if y == "y"; x +>> > "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
Christoph Guest



Reply With Quote

