Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Overridable method for string interpolation #1928
This is really a Ruby language design bug, but I'd like to get some feedback from the smart folks here before going to ruby-lang.
I love programming in Ruby, and I really like being able to override everything, down to
Right now, it seems that both MRI and Rubinius build the string fragments and use a special VM bytecode for concatenating the fragments. I would like to have this opcode be replaced by a call to
I realize that this adds an extra method call, and method dispatch is expensive. At the same time, most string interpolation cases have at least one
I would like to get your opinions on this design. A colleague of mine will be providing an implementation once we have a reasonable design.
Here's my motivation, in case you're wondering why I'm putting together this proposal. I'm trying to implement a dataflow analysis tool that adds "labels" to objects, and propagates these labels throughout a program's dataflow. For example, if
Also, here is a paper that ran into the same roadblock. See page 9, section "String interpolation".
String interpolation could be essentially
One problem is that since #to_s can be overridden, we have to be able to force an object into some sort of String representation.
Personally, I'm not very interested in this proposal. Any attempt to layer diagnostics or tooling on top of methods is bound to encounter "invisible" parts of the runtime. It's a beguiling invitation that Ruby allows most methods to be overridden. That facility enables writing very good and clean OO code. To have good diagnostics requires support from the runtime system. I'd rather see standard APIs developed there (eg for Debuggers, Coverage tools, GC stats, etc).
It would certainly be possible to implement, perhaps with a VM flag to enable or disable the behavior, but it's not something I'd ever do in JRuby by default.
JRuby has gone to great lengths to eliminate transient objects from string interpolation, in some cases only creating a single string for the result if the interpolated expressions are known types. Ultimately, any attempt to make interpolation call String methods would add more transient objects and more dynamic calls to what's currently a pretty fast implementation.