Hi --

On Mon, 3 Nov 2008, Yukihiro Matsumoto wrote:

> Hi,
>
> In message "Re: [ruby-core:19660] Odd TypeError in inject (1.9.1 preview 1)"
>    on Fri, 31 Oct 2008 20:20:56 +0900, "David A. Black" <dblack / rubypal.com> writes:
>
> |I think this should return 0. Anything I'm missing?
> |
> |>> RUBY_DESCRIPTION
> |=> "ruby 1.9.1 (2008-10-28 revision 19983) [i386-darwin9.5.0]"
> |>> [].inject(0)
> |TypeError: 0 is not a symbol
> | 	from (irb):4:in `inject'
> | 	from (irb):4
> | 	from /usr/local/bin/irb19:12:in `<main>'
>
> According to [ruby-core:19666], you expect [].inject(0) to work as
>
>   obj.enum_for(:inject, other_object)
>
> right?  But the current behavior of enumerator only works right for
> iterators that do not use the values from blocks. e.g. #each.  So,
> enumerators do not work for #inject (and #map).  So expecting
> something do not work is kinda weird.

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).

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?


David

-- 
Rails training from David A. Black and Ruby Power and Light:
   Intro to Ruby on Rails  January 12-15   Fort Lauderdale, FL
   Advancing with Rails    January 19-22   Fort Lauderdale, FL *
   * Co-taught with Patrick Ewing!
See http://www.rubypal.com for details and updates!