Skip to content

Loading…

TypeError in string interpolation (when #to_s does not return a String) #34

Closed
brixen opened this Issue · 4 comments

3 participants

@brixen
Rubinius member

LH 765 by shugo.

Rubinius handles the following script differently from MRI. Is it intensional?

$ cat t.rb
x = Object.new
def x.to_s
  return 1
end
p "#{x}"
$ ruby-1_8_6 -v t.rb
ruby 1.8.6 (2008-08-08 patchlevel 286) [i686-linux]
"#<Object:0xb7dedbd8>"
$ rbx -v t.rb     
rubinius 0.11.0-dev (ruby 1.8.6) (eabf5ec7d 12/31/2009) [i686-pc-linux-gnu]
An exception has occurred:
      Tried to use non-reference value 0x3 as type String (48) (TypeError)

Backtrace:
            Compiler::Utils.single_load at kernel/compiler/compile.rb:245
    Compiler::Utils.load_from_extension at kernel/compiler/compile.rb:323
                      Object#__script__ at kernel/loader.rb:240
@burke

http://github.com/evanphx/rubinius/blob/65944505d2f2a34d29944d27457a0479cbf0ffb0/lib/compiler/ast/literals.rb#L289

  # We also do not handle any case where #to_s does not actually return a
  # String instance.

So yes, it seems that it's intentional.

@burke

Umm, just realized you wrote the comment I quoted. So uhh.. yeah.

@evanphx
Rubinius member

This is particularly hairy because we call #to_s in the bytecode, so by the time the string construction starts, we've lost the object we called #to_s on to use the fallback.

We'd have to call out to a helper that calls #to_s and checks the return value. Thats expensive since this almost never happens anyway.

I wonder if I shouldn't just take the value, and if it's not a string, return it as one. So in your example case, the value would be "1", since we'd render the 1.

Perhaps I'll just do that.

@evanphx
Rubinius member

Use a fallback conversion in string_build. Closed by 33daa62.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.