This project is archived and is in readonly mode.

#2959 ✓committed
Akira Matsuda

Ruby 1.9.2 compat: add to_str to ColumnDefinition

Reported by Akira Matsuda | July 27th, 2009 @ 12:45 AM | in 2.3.4

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.

Comments and changes to this ticket

  • Akira Matsuda

    Akira Matsuda July 27th, 2009 @ 04:06 PM

    • 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
  • Nikolay Petrachkov

    Nikolay Petrachkov August 5th, 2009 @ 09:31 AM

    • 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

  • Jeremy Kemper

    Jeremy Kemper August 5th, 2009 @ 07:43 PM

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

  • Akira Matsuda

    Akira Matsuda August 6th, 2009 @ 12:44 AM

    Right.

    Updated the patch not to use to_str nor to_s.

  • Repository

    Repository August 6th, 2009 @ 01:11 AM

    • 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 state:committed]

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

  • Repository

    Repository August 6th, 2009 @ 01:11 AM

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

    [#2959 state:committed]

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

  • Jeremy Kemper

    Jeremy Kemper August 6th, 2009 @ 01:36 AM

    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"
    
  • Sakuro

    Sakuro August 6th, 2009 @ 01:48 AM

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

  • Sakuro

    Sakuro August 6th, 2009 @ 01:51 AM

    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!"
    
  • Jeremy Kemper

    Jeremy Kemper August 6th, 2009 @ 04:41 AM

    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.

  • Jeremy Kemper

    Jeremy Kemper August 6th, 2009 @ 04:48 AM

    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
    
  • Jeremy Kemper

    Jeremy Kemper August 6th, 2009 @ 04:56 AM

    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.

  • Jeremy Kemper

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Referenced by

Pages