Robert Klemme
10/7/2003 12:53:00 PM
"Yukihiro Matsumoto" <matz@ruby-lang.org> schrieb im Newsbeitrag
news:1065498757.424696.31758.nullmailer@picachu.netlab.jp...
> Hi,
>
> In message "Re: Enumerable#inject is surprising me..."
> on 03/10/07, nobu.nokada@softhome.net <nobu.nokada@softhome.net>
writes:
>
> |> ['a'].inject(''){break 'b'} # => 'b'
> |
> |But this doesn't work as Nathaniel expected. It returns last
> |iteration value, "".
>
> Commit the fix, please.
Could you please explain why there is something that needs fixing? To me
this seems perfecly ok. I don't know what's wrong here:
irb(main):001:0> a=(1..10).to_a
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
irb(main):002:0> a.inject {|*args| p args}
[1, 2]
[nil, 3]
[nil, 4]
[nil, 5]
[nil, 6]
[nil, 7]
[nil, 8]
[nil, 9]
[nil, 10]
=> nil
irb(main):003:0> a.inject("*") {|*args| p args}
["*", 1]
[nil, 2]
[nil, 3]
[nil, 4]
[nil, 5]
[nil, 6]
[nil, 7]
[nil, 8]
[nil, 9]
[nil, 10]
=> nil
irb(main):004:0> [].inject("*") {|*args| p args}
=> "*"
irb(main):005:0> [].inject {|*args| p args}
=> nil
irb(main):006:0> [1].inject {|*args| p args}
=> 1
irb(main):007:0> [1].inject("*") {|*args| p args}
["*", 1]
=> nil
irb(main):008:0> [1].inject("*") {|*args| break args}
=> ["*", 1]
irb(main):009:0> [].inject(){|*args| break args}
=> nil
irb(main):010:0> ['a'].inject(){|*args| break args}
=> "a"
irb(main):011:0> ['a'].inject(''){|*args| break args}
=> ["", "a"]
irb(main):012:0>
Thanks!
robert