Ruby Forum Ruby-core > Rails broken with 1.8.7 bc Symbol#to_proc

Posted by Ola Bini (Guest)
on 17.04.2008 12:13
(Received via mailing list)
Hi,

Not sure if anyone has noticed this, but Rails doesn't startup with the
1.8.7-preview. This is because of the native Symbol#to_proc that seems
to have a slightly different behavior than the Ruby-implemented one.
Example:

on 1.8.6 this:
class Symbol
 def to_proc
   proc { |*args| args.shift.__send__(self, *args) }
 end
end

p([[proc{ },1]].each(&:call))

Will fail with:
t1.rb:3:in `__send__': undefined method `call' for
[#<Proc:0x00000000@t1.rb:7>, 1]:Array (NoMethodError)
        from t1.rb:3:in `to_proc'
        from t1.rb:7:in `each'
        from t1.rb:7

While with 1.8.7-preview this line
 p([[proc{ },1]].each(&:call))

will not fail, but print
[[1]]

The error Rails gives is related:
/Users/olabini/workspace/optimize_jruby/vendor/rails/activesupport/lib/active_support/inflector.rb:257:in
`object_id': wrong number of arguments (1 for 0) (ArgumentError)
        from
/Users/olabini/workspace/optimize_jruby/vendor/rails/activesupport/lib/active_support/inflector.rb:257:in
`to_proc'
        from
/Users/olabini/workspace/optimize_jruby/vendor/rails/activesupport/lib/active_support/dependencies.rb:377:in
`collect'
        from
/Users/olabini/workspace/optimize_jruby/vendor/rails/activesupport/lib/active_support/dependencies.rb:377:in
`new_constants_in'
        from
/Users/olabini/workspace/optimize_jruby/vendor/rails/activesupport/lib/active_support/dependencies.rb:496:in
`require'
        from
/Users/olabini/workspace/optimize_jruby/vendor/rails/activesupport/lib/active_support.rb:36
        from
/Users/olabini/workspace/optimize_jruby/vendor/rails/railties/lib/commands/server.rb:1:in
`require'
        from
/Users/olabini/workspace/optimize_jruby/vendor/rails/railties/lib/commands/server.rb:1
        from script/server:3:in `require'
        from script/server:3


Cheers

--
 Ola Bini (http://ola-bini.blogspot.com)
 JRuby Core Developer
 Developer, ThoughtWorks Studios (http://studios.thoughtworks.com)
 Practical JRuby on Rails (http://apress.com/book/view/9781590598818)

 "Yields falsehood when quined" yields falsehood when quined.
Posted by Akinori MUSHA (Guest)
on 17.04.2008 13:53
(Received via mailing list)
At Thu, 17 Apr 2008 19:13:17 +0900,
Ola Bini wrote:
> Not sure if anyone has noticed this, but Rails doesn't startup with
> the 1.8.7-preview. This is because of the native Symbol#to_proc that
> seems to have a slightly different behavior than the Ruby-implemented
> one.

Thank you for the report.  We recognize the problem and will be fixing
it shortly.

http://rubyforge.org/tracker/index.php?func=detail&aid=19551&group_id=426&atid=22040

It would be an option to bring the Rails' code in if the right way to
fix the current implementation were to cost too much.

>
> will not fail, but print
> [[1]]
>
> The error Rails gives is related:
> /Users/olabini/workspace/optimize_jruby/vendor/rails/activesupport/lib/active_support/inflector.rb:257:in
> `object_id': wrong number of arguments (1 for 0) (ArgumentError)
>        from

Yes, this behavior reflects the messy argument list handling in Ruby
<=1.8 and we'll have to work it around.

Regards,
Posted by Ola Bini (Guest)
on 17.04.2008 14:03
(Received via mailing list)
Akinori MUSHA wrote:
> fix the current implementation were to cost too much.
>
>   
Not sure if that's a good option. Rails is just the most obvious
failure. I would imagine there is loads of Symbol#to_proc code in many
places that will fail the same way.

> Yes, this behavior reflects the messy argument list handling in Ruby
> <=1.8 and we'll have to work it around.
>
>   
Yeah, JRuby has the same problem with the native version, actually...

Cheers

--
 Ola Bini (http://ola-bini.blogspot.com)
 JRuby Core Developer
 Developer, ThoughtWorks Studios (http://studios.thoughtworks.com)
 Practical JRuby on Rails (http://apress.com/book/view/9781590598818)

 "Yields falsehood when quined" yields falsehood when quined.
Posted by Akinori MUSHA (Guest)
on 17.04.2008 15:14
(Received via mailing list)
At Thu, 17 Apr 2008 21:02:56 +0900,
Ola Bini wrote:
> > It would be an option to bring the Rails' code in if the right way to
> > fix the current implementation were to cost too much.
> >
> >
> Not sure if that's a good option. Rails is just the most obvious
> failure. I would imagine there is loads of Symbol#to_proc code in many
> places that will fail the same way.

You may be right, while there is a good use for the method also.

Let me consider a while.

> > Yes, this behavior reflects the messy argument list handling in Ruby
> > <=1.8 and we'll have to work it around.
> >
> >
> Yeah, JRuby has the same problem with the native version, actually...

Good news is it got better in 1.9, bad news is it is almost impossible
to bring the changes in by now.
Posted by Jeremy Kemper (Guest)
on 18.04.2008 00:12
(Received via mailing list)
On Thu, Apr 17, 2008 at 3:13 AM, Ola Bini <ola.bini@gmail.com> wrote:
>  Not sure if anyone has noticed this, but Rails doesn't startup with the
> 1.8.7-preview. This is because of the native Symbol#to_proc that seems to
> have a slightly different behavior than the Ruby-implemented one.

It also differs from 1.9.

>
>  Will fail with:
>  t1.rb:3:in `__send__': undefined method `call' for
> [#<Proc:0x00000000@t1.rb:7>, 1]:Array (NoMethodError)
>        from t1.rb:3:in `to_proc'
>        from t1.rb:7:in `each'
>        from t1.rb:7

1.9's native Symbol#to_proc gives the same error.

>  While with 1.8.7-preview this line
>  p([[proc{ },1]].each(&:call))
>
>  will not fail, but print
>  [[1]]



Index: test/ruby/test_symbol.rb
===================================================================
--- test/ruby/test_symbol.rb  (revision 16065)
+++ test/ruby/test_symbol.rb  (working copy)
@@ -77,6 +77,7 @@

   def test_to_proc
     assert_equal %w(1 2 3), (1..3).map(&:to_s)
+    assert_equal %w(one two three), { 1 => "one", 2 => "two", 3 =>
"three" }.sort_by(&:first).map(&:last)
     assert_nothing_raised(ArgumentError) { :object_id.to_proc.call([]) 
}
     assert_nothing_raised(ArgumentError) { :object_id.to_proc.call([1]) 
}
     assert_nothing_raised(ArgumentError) { 
:object_id.to_proc.call([1,2]) }


jeremy@geech ruby_1_8 $ ruby -v
-r~/rails/activesupport/lib/active_support test/ruby/test_symbol.rb
ruby 1.8.6 (2007-09-24 patchlevel 111) [universal-darwin9.0]
[...] 5 tests, 50 assertions, 0 failures, 0 errors
$ /usr/local/bin/ruby-trunk -v test/ruby/test_symbol.rb
ruby 1.9.0 (2008-04-18 revision 0) [i686-darwin9.2.2]
[...] 5 tests, 50 assertions, 0 failures, 0 errors
$ /usr/local/bin/ruby -v test/ruby/test_symbol.rb
ruby 1.8.7 (2008-04-17 patchlevel 5000) [i686-darwin9.2.2]
[...] 1) Error:
test_to_proc(TestSymbol):
NoMethodError: undefined method `first' for 1:Fixnum
    test/ruby/test_symbol.rb:58:in `to_proc'
    test/ruby/test_symbol.rb:58:in `sort_by'
    test/ruby/test_symbol.rb:80:in `each'
    test/ruby/test_symbol.rb:80:in `sort_by'
    test/ruby/test_symbol.rb:80:in `test_to_proc'

jeremy