Hi,
In message "Re: [ruby-core:19683] Re: Odd TypeError in inject (1.9.1 preview 1)"
on Mon, 3 Nov 2008 23:36:38 +0900, "David A. Black" <dblack / rubypal.com> writes:
|I think I was starting from the 1.8 behavior, and not grasping the
|thing about the magic symbol-test. So I expected it to return 0, and
|it was the symbol complaint that originally struck me as odd (and I'm
|still not sure it makes complete sense to me).
Indeed 1.8.6 returns 0 for [].inject(0), but it's kinda odd corner
case. 1.8.6 inject is inconsistent.
1.8.6 1.8.7 1.9.1
[].inject(0) 0 error error
[1].inject(0) error error error
[].inject nil error error
[1].inject 1 error error
I think the behavior of #inject should be more consistent than 1.8.6.
The possible alternative behavior is
[].inject(0) enumerator
[1].inject(0) enumerator
[].inject enumerator
[1].inject enumerator
I am not sure how much it is useful.
|On the general subject of #inject and enumerators: what about this?
|
|>> a
|=> [1, 2, 3, 4]
|>> e = a.enum_for(:inject, Set.new)
|=> #<Enumerator:0x458f08>
|>> e.each {|s,i| s << i }
|=> #<Set: {1, 2, 3, 4}>
|
|This #each does return a value from the block, because it's really a
|front-end to #inject. Or am I misunderstanding what you're saying
|about #inject not working with enumerators?
I meant generator like behavior introduced in 1.9. #next receives a
value from the iterator but there's no way to give a value back from.
matz.