From: Jacob Fugal Date: 2008-11-01T00:26:34+09:00 Subject: [ruby-core:19665] Re: Odd TypeError in inject (1.9.1 preview 1) ------=_Part_35097_15180740.1225466854469 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline On Fri, Oct 31, 2008 at 8:40 AM, Nobuyoshi Nakada 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 `
' > > > > 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". Jacob Fugal ------=_Part_35097_15180740.1225466854469 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline

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

Jacob Fugal
------=_Part_35097_15180740.1225466854469--