Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Ruby 1.9.2 compat: add to_str to ColumnDefinition

#2959

rake db:migrate doesn't work on Ruby 1.9.2 because Array#* uses to_str instead of to_s to join values since Ruby 1.9.2.

Attached a patch to add to_str to ColumnDefinition which applies to master and 2-3-stable.

Reported by Akira Matsuda · July 27th, 2009 @ 12:45 AM

State: committed
Milestone: 2.3.4
Assigned to: Jeremy Kemper Jeremy Kemper
Importance: none

Activity

  1. Akira Matsuda
    Akira Matsuda
    • Tag changed from 2-3-stable, edge, migrate, rake, ruby1.9.2, schema to 2-3-stable, edge, migrate, patch, rake, ruby1.9.2, schema

    July 27th, 2009 @ 04:06 PM

  2. Nikolay Petrachkov
    Nikolay Petrachkov
    • Assigned user set to Jeremy Kemper

    ruby -v:
    ruby 1.9.2dev (2009-08-01 trunk 24347) [x86_64-linux]
    rake test_postgresql
    Before patch:

    2098 tests, 4090 assertions, 13 failures, 987 errors, 0 pendings, 0 omissions, 0 notifications

    After patch:

    2098 tests, 6860 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications

    August 5th, 2009 @ 09:31 AM

  3. Jeremy Kemper
    Jeremy Kemper
    • State changed from new to open
    • Milestone changed from 2.x to 2.3.4

    But ColumnDefinition really is not string-like. Is using to_str appropriate?

    Perhaps we should not use Array#* instead.

    August 5th, 2009 @ 07:43 PM

  4. Akira Matsuda
    Akira Matsuda

    Right.

    Updated the patch not to use to_str nor to_s.

    August 6th, 2009 @ 12:44 AM

  5. Repository
    Repository
    • State changed from open to committed

    (from [230d43fbf5dee569ea031c8c394ba9ce70804cae]) Ruby 1.9.2 compat: Array#* uses to_str instead of to_s to join values since Ruby 1.9.2

    [#2959 Ruby 1.9.2 compat: add to_str to ColumnDefinition state:committed]

    Signed-off-by: Jeremy Kemper jeremy@bitsweat.net
    http://github.com/rails/rails/commit/230d43fbf5dee569ea031c8c394ba9...

    August 6th, 2009 @ 01:11 AM

  6. Repository
  7. Jeremy Kemper
    Jeremy Kemper

    I checked on Ruby 1.9.2 and it appears to still use to_s. Did I miss this change?

    $ ruby19 -v
    ruby 1.9.2dev (2009-08-06) [i386-darwin9.7.0]
    $ irb19
    >> class Foo; def to_s; 'bar' end end
    => nil
    >> [Foo.new, Foo.new] * ' '
    => "bar bar"
    

    August 6th, 2009 @ 01:36 AM

  8. Sakuro
    Sakuro

    Is something redefined in Struct? (ColumnDefinition is a Struct)

    $ irb --prompt=simple

    RUBY_DESCRIPTION => "ruby 1.9.2dev (2009-07-31 trunk 24341) [i386-darwin9.7.0]" X = Class.new => X [X.new, X.new] * '--' => "#<X:0x2e0f04>--#<X:0x2e0ec8>" Y = Struct.new(:a, :b) => Y [Y.new, Y.new] * '--' => "------" # 3 separators ?? class Y; def to_s; 'hi!'; end; end => nil [Y.new, Y.new] * '--' => "------" class Y; def to_str; 'hola!'; end; end => nil [Y.new, Y.new] * '--' => "hola!--hola!"

    August 6th, 2009 @ 01:48 AM

  9. Sakuro
    Sakuro

    bah. Hope this formats correctly.

    $ irb --prompt=simple
    >> RUBY_DESCRIPTION
    => "ruby 1.9.2dev (2009-07-31 trunk 24341) [i386-darwin9.7.0]"
    >> X = Class.new
    => X
    >> [X.new, X.new] * '--'
    => "#<X:0x2e0f04>--#<X:0x2e0ec8>"
    >> Y = Struct.new(:a, :b)
    => Y
    >> [Y.new, Y.new] * '--'
    => "------"
    >> class Y; def to_s; 'hi!'; end; end
    => nil
    >> [Y.new, Y.new] * '--'
    => "------"
    >> class Y; def to_str; 'hola!'; end; end
    => nil
    >> [Y.new, Y.new] * '--'
    => "hola!--hola!"
    

    August 6th, 2009 @ 01:51 AM

  10. Jeremy Kemper
    Jeremy Kemper

    Yes, it's because of Struct#to_a

    
    >> Bar = Struct.new(:a, :b)
    => Bar
    >> bars = [Bar.new('1', '2'), Bar.new('3', '4')]
    => [#<struct Bar a="1", b="2">, #<struct Bar a="3", b="4">]
    >> bars * '--'
    => "1--2--3--4"
    >> class Bar; def to_s; 'foo' end end
    => nil
    >> bars * '--'
    => "1--2--3--4"
    >> class Bar; undef_method :to_a end
    => Bar
    >> bars * '--'
    => "#<struct Bar a=\"1\", b=\"2\">--#<struct Bar a=\"3\", b=\"4\">"
    >> class Bar; def to_str; 'foo' end end
    => nil
    >> bars * '--'
    => "foo--foo"
    

    To see why, check ary_join_1:

    
              default:
                tmp = rb_check_string_type(val);
                if (!NIL_P(tmp)) {
                    val = tmp;
                    goto str_join;
                }
                tmp = rb_check_convert_type(val, T_ARRAY, "Array", "to_a");
                if (!NIL_P(tmp)) {
                    obj = val;
                    val = tmp;
                    goto ary_join;
                }
    

    rb_check_convert_type catches Struct since it's T_DATA, so it's incorrectly treated as joining a recursive array.

    I think this is a Ruby bug.

    August 6th, 2009 @ 04:41 AM

  11. Jeremy Kemper
    Jeremy Kemper

    Actually, it's intentional:

    commit d32b853e4502e385372e5ae45503290df824518e
    Author: matz 
    Date:   Fri Jul 3 18:14:33 2009 +0000
    
        * enum.c (enum_join): add Enumerable#join.
        
        * array.c (ary_join_1): recursive join for Enumerators (and
          objects with #to_a).
        
        * array.c (rb_ary_join): performance tune.
        
        git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@23951 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
    

    August 6th, 2009 @ 04:48 AM

  12. Jeremy Kemper
    Jeremy Kemper

    Oops, I cut and pasted too much.

    
    >> class Bar; undef_method :to_a end
    => Bar
    >> bars * '--'
    => "#<struct Bar a=\"1\", b=\"2\">--#<struct Bar a=\"3\", b=\"4\">"
    

    above actually results in "foo--foo" since to_s is defined.

    August 6th, 2009 @ 04:56 AM

  13. Jeremy Kemper