Hi --

On Sat, 1 Nov 2008, Jacob Fugal wrote:

> On Fri, Oct 31, 2008 at 8:40 AM, Nobuyoshi Nakada <nobu / ruby-lang.org>wrote:
>
>> Hi,
>>
>> At Fri, 31 Oct 2008 23:17:47 +0900,
>> Jacob Fugal wrote in [ruby-core:19663]:
>>>>> [].inject(0)
>>>>>>
>>>>> TypeError: 0 is not a symbol
>>>>        from (irb):4:in `inject'
>>>>        from (irb):4
>>>>        from /usr/local/bin/irb19:12:in `<main>'
>>>
>>> Seems to me it's trying to treat 0 as a symbol-to-procable block
>> parameter,
>>> then failing during the symbol-to-proc conversion. Still a bug, though,
>> IMO.
>>
>> Then what do you expect to be called?
>>
>
> I expected, as I suspect David did, that the method would either complain
> explicitly about a missing block parameter, or ignore the missing block
> parameter since it's unused (the array is empty). In my mind, I was thinking
> the method should only try and treat the first (or is it last, I don't
> remember which it is) parameter as a to_proc-able Symbol if there are two
> non-block arguments. I expected a single parameter to be interpreted as
> "initial" rather than "block".
>
> Thinking about it more, I realize that would make the ary.inject(:foo) usage
> inconsistent, as :foo would be interpreted as "initial" rather than "block".
> The error is basically saying: "You didn't provide a block, so I was going
> to try using the first argument as a proc, but it wasn't a proc, nor was it
> a Symbol that I could convert into a proc." -- so it *was* complaining about
> a missing block parameter, but only implicitly.
>
> Perhaps rather than checking for a Symbol explicitly, just check for
> #responds_to?(:to_proc)? If it does, call it and use the result as the
> block. If not, the error message could say something like "TypeError: 0
> cannot be converted to a Proc".

I thought that the idiom was (&:sym), not just (:sym). I certainly
would expect a non-& argument to inject to be the initial value for
the accumulator and not magically converted to something else. If
there's no block I think I would expect this:

   obj.inject(other_object)

to mean this:

   obj.enum_for(:inject, other_object)

Special-casing symbols like that seems much too magic and restrictive
to me.


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!