Enumerable#inject is surprising me...<Pine.LNX.4.44.0310090713420.21222-100000@ool-4355dfae.dyn.optonline.net>

Ask a Question related to Ruby, Design and Development.

  1. #1

    Default 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:
    > >>>>> "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.
    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?


    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

  2. Similar Questions and Discussions

    1. 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}")...
    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...
    3. 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...
    4. 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
    5. 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...
  3. #2

    Default Re: Enumerable#inject is surprising me... <Pine.LNX.4.44.0310090713420.21222-100000@ool-4355dfae.dyn.optonline.net>

    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


    Christoph Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139