From: Jacob Fugal Date: 2008-11-01T01:51:53+09:00 Subject: [ruby-core:19667] Re: Odd TypeError in inject (1.9.1 preview 1) ------=_Part_37612_15120926.1225471974095 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline On Fri, Oct 31, 2008 at 10:08 AM, David A. Black wrote: > On Sat, 1 Nov 2008, Jacob Fugal wrote: > > 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. I agree wholeheartedly. I was not previously aware of a obj.inject(:sym) usage, but that's the only sense I could make of the error. My original off-the-cuff hypothesis that the error stemmed from inside Symbol#to_proc makes absolutely no sense on second inspection, so it must be the inject method that's checking for a symbol. Sure enough, the rdoc at http://redmine.ruby-lang.org/repositories/entry/ruby-19/enum.c#L449indicates such special treatment for Symbols as the sole argument and the error raised stems from the call to rb_to_id that occurs at line 507. Jacob Fugal ------=_Part_37612_15120926.1225471974095 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline On Fri, Oct 31, 2008 at 10:08 AM, David A. Black <dblack@rubypal.com> wrote:
On Sat, 1 Nov 2008, Jacob Fugal wrote:

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.

I agree wholeheartedly.

I was not previously aware of a obj.inject(:sym) usage, but that's the only sense I could make of the error. My original off-the-cuff hypothesis that the error stemmed from inside Symbol#to_proc makes absolutely no sense on second inspection, so it must be the inject method that's checking for a symbol. Sure enough, the rdoc at http://redmine.ruby-lang.org/repositories/entry/ruby-19/enum.c#L449 indicates such special treatment for Symbols as the sole argument and the error raised stems from the call to rb_to_id that occurs at line 507.

Jacob Fugal
------=_Part_37612_15120926.1225471974095--