Skip to content


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

brixen opened this Issue · 4 comments

3 participants

Rubinius member

LH 765 by shugo.

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

$ cat t.rb
x =
def x.to_s
  return 1
p "#{x}"
$ ruby-1_8_6 -v t.rb
ruby 1.8.6 (2008-08-08 patchlevel 286) [i686-linux]
$ 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)

            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

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


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

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.

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.