# Coopting String interpolation

**URL:** <https://rubytalk.org/t/coopting-string-interpolation/37654>\
**Category:** ruby-talk\
**Created:** [17 May 2007 04:20 UTC](https://rubytalk.org/t/coopting-string-interpolation/37654 "2007-05-17T04:20:14Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aureliano\_Calvo](https://avatars.discourse-cdn.com/v4/letter/a/f04885/32.png) [@Aureliano\_Calvo](https://rubytalk.org/u/Aureliano_Calvo)\
**Post date:** [17 May 2007 04:20 UTC](https://rubytalk.org/t/coopting-string-interpolation/37654/1 "2007-05-17T04:20:14Z")

</div>

We all know the #{} operator for String interpolation.  
It's used like this:

name="Aure"  
greeting="hi, #{name}"

and greeting evaluates to "hi, Aure". But I would like to change it.  
Is there a pure Ruby way to force "hi, #{name}" to evaluate to "hi,  
AURE" (get the uppercase of the original interpolated String).  
Something like:

class String  
&nbsp;&nbsp;&nbsp;# do some magic here  
end

name="Aure"  
greeting="hi, #{name}"

and now greeting evaluates to "hi, AURE". Is there a method in String  
to override in order to take control of the String interpolation?

Thanks in advance,  
Aureliano.

---

<div class="post-metadata">

**Author:** ![\_Pena\_Botp1](https://avatars.discourse-cdn.com/v4/letter/_/94ad74/32.png) [@\_Pena\_Botp1](https://rubytalk.org/u/_Pena_Botp1)\
**Post date:** [17 May 2007 04:29 UTC](https://rubytalk.org/t/coopting-string-interpolation/37654/2 "2007-05-17T04:29:18Z")

</div>

# Is there a pure Ruby way to force "hi, #{name}" to evaluate to "hi,  
# AURE" (get the uppercase of the original interpolated String).  
# Something like:

pols. think simple, then just do it.

interpolation can be any expression..

irb(main):001:0\> name="Aure"  
=\> "Aure"  
irb(main):002:0\> greeting="hi, #{name}"  
=\> "hi, Aure"  
irb(main):003:0\> greeting="hi, #{name.upcase}"  
=\> "hi, AURE"  
irb(main):005:0\> greeting="hi, #{"Ms "+name.capitalize}"  
=\> "hi, Ms Aure"  
irb(main):006:0\>

> **···**
>
> From: aurelianito [[mailto:aurelianocalvo@gmail.com](mailto:aurelianocalvo@gmail.com)] :

---

<div class="post-metadata">

**Author:** ![Nobuyoshi\_Nakada1](https://avatars.discourse-cdn.com/v4/letter/n/9f8e36/32.png) [@Nobuyoshi\_Nakada1](https://rubytalk.org/u/Nobuyoshi_Nakada1)\
**Post date:** [17 May 2007 07:15 UTC](https://rubytalk.org/t/coopting-string-interpolation/37654/3 "2007-05-17T07:15:05Z")

</div>

Hi,

At Thu, 17 May 2007 13:20:14 +0900,  
aurelianito wrote in [ruby-talk:251879]:

> class String  
> &nbsp;&nbsp;&nbsp;# do some magic here  
> end
> 
> name="Aure"  
> greeting="hi, #{name}"
> 
> and now greeting evaluates to "hi, AURE". Is there a method in String  
> to override in order to take control of the String interpolation?

If the value isn't a String, to\_s is called for it to get a  
String. Otherwise, the content is interpolated directly.  
There is not a hook you want.

> **···**
>
> --  
> Nobu Nakada

---

<div class="post-metadata">

**Author:** ![Gary\_Wright](https://avatars.discourse-cdn.com/v4/letter/g/58956e/32.png) [@Gary\_Wright](https://rubytalk.org/u/Gary_Wright)\
**Post date:** [17 May 2007 23:58 UTC](https://rubytalk.org/t/coopting-string-interpolation/37654/4 "2007-05-17T23:58:22Z")

</div>

I just released my Jig package that might be helpful to you.

require 'rubygems'  
require 'jig'

# Create a named 'gap' that upcases everything  
upcase = Jig::Gap.new(:name) { |x| x.upcase }

# Create a jig consisting of a string, and the customize 'upcasing' gap.  
greeting = Jig["hi, ", upcase]

# Fill the gap with data to generate a new jig.  
# puts calls Jig#to\_s to convert the new jig to a string

puts greeting % { :name =\> "Aure" } # =\> hi, AURE  
puts greeting % { :name =\> "Gary" } # =\> hi, GARY

> **···**
>
> On May 16, 2007, at 9:20 PM, aurelianito wrote:
> 
> > We all know the #{} operator for String interpolation.  
> > It's used like this:
> > 
> > name="Aure"  
> > greeting="hi, #{name}"
> > 
> > and greeting evaluates to "hi, Aure". But I would like to change it.  
> > Is there a pure Ruby way to force "hi, #{name}" to evaluate to "hi,  
> > AURE" (get the uppercase of the original interpolated String).

---

<div class="post-metadata">

**Author:** ![\_Pena\_Botp1](https://avatars.discourse-cdn.com/v4/letter/_/94ad74/32.png) [@\_Pena\_Botp1](https://rubytalk.org/u/_Pena_Botp1)\
**Post date:** [17 May 2007 04:33 UTC](https://rubytalk.org/t/coopting-string-interpolation/37654/5 "2007-05-17T04:33:26Z")

</div>

# irb(main):005:0\> greeting="hi, #{"Ms "+name.capitalize}"  
# =\> "hi, Ms Aure"

sorry, was too fast on the last example; should be..

irb(main):006:0\> greeting="hi, #{name="Anne";"Ms "+name.capitalize}"  
=\> "hi, Ms Anne"

---

<div class="post-metadata">

**Author:** ![Aureliano\_Calvo](https://avatars.discourse-cdn.com/v4/letter/a/f04885/32.png) [@Aureliano\_Calvo](https://rubytalk.org/u/Aureliano_Calvo)\
**Post date:** [17 May 2007 05:09 UTC](https://rubytalk.org/t/coopting-string-interpolation/37654/6 "2007-05-17T05:09:55Z")

</div>

I'm trying to do some metaprogramming and I need to apply some  
operation to all the strings that are interpolated. But I can't change  
the interpolation. In the example, I would like that "hi, #{name}" to  
evaluate to "hi, AURE" instead of "hi, Aure". I know that inside the  
#{} "operator" I can put any ruby code, so "hi, #{name.capitalize}"  
would evaluate to what I want.

But I want it to execute code that I DON'T write there. Ideally, there  
should be a method hook or something to change the way the  
interpolation works. But I couldn't find it :(. May be a different  
example may be more clear. How can I do to write to a file all the  
strings generated via interpolations (id est, all the strings that are  
generated evaluating the different #{} "operators" in a program)?

Thanks,  
Aureliano.

> **···**
>
> On 17 mayo, 01:33, Peña, Botp \<b...@delmonte-phil.com\> wrote:
> 
> > # irb(main):005:0\> greeting="hi, #{"Ms "+name.capitalize}"  
> > # =\> "hi, Ms Aure"
> > 
> > sorry, was too fast on the last example; should be..
> > 
> > irb(main):006:0\> greeting="hi, #{name="Anne";"Ms "+name.capitalize}"  
> > =\> "hi, Ms Anne"

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [17 May 2007 10:50 UTC](https://rubytalk.org/t/coopting-string-interpolation/37654/7 "2007-05-17T10:50:03Z")

</div>

Frankly, it has not become clear to me what you are up to. Can you maybe just state which problem you are trying to solve?

As for a solution to your original example, you can do something like this - but it's a hack and not very reliable:

class Env  
&nbsp;&nbsp;&nbsp;def initialize(vals)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@vals = vals  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;def process(&b)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance\_eval(&b)  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;def method\_missing(s,\*a,&b)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super unless a.empty?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@vals[s.to\_sym].upcase  
&nbsp;&nbsp;&nbsp;end  
end

irb(main):015:0\> e=Env.new(:name=\>'foo')  
=\> #\<Env:0x7ff6d388 @vals={:name=\>"foo"}\>  
irb(main):016:0\> e.process { "bar #{name}" }  
=\> "bar FOO"  
irb(main):017:0\>

Kind regards

&nbsp;&nbsp;robert

> **···**
>
> On 17.05.2007 07:09, aurelianito wrote:
> 
> > On 17 mayo, 01:33, Peña, Botp \<b...@delmonte-phil.com\> wrote:
> > 
> > > # irb(main):005:0\> greeting="hi, #{"Ms "+name.capitalize}"  
> > > # =\> "hi, Ms Aure"
> > > 
> > > sorry, was too fast on the last example; should be..
> > > 
> > > irb(main):006:0\> greeting="hi, #{name="Anne";"Ms "+name.capitalize}"  
> > > =\> "hi, Ms Anne"
> > 
> > I'm trying to do some metaprogramming and I need to apply some  
> > operation to all the strings that are interpolated. But I can't change  
> > the interpolation. In the example, I would like that "hi, #{name}" to  
> > evaluate to "hi, AURE" instead of "hi, Aure". I know that inside the  
> > #{} "operator" I can put any ruby code, so "hi, #{name.capitalize}"  
> > would evaluate to what I want.
> > 
> > But I want it to execute code that I DON'T write there. Ideally, there  
> > should be a method hook or something to change the way the  
> > interpolation works. But I couldn't find it :(. May be a different  
> > example may be more clear. How can I do to write to a file all the  
> > strings generated via interpolations (id est, all the strings that are  
> > generated evaluating the different #{} "operators" in a program)?

---

<div class="post-metadata">

**Author:** ![Aureliano\_Calvo](https://avatars.discourse-cdn.com/v4/letter/a/f04885/32.png) [@Aureliano\_Calvo](https://rubytalk.org/u/Aureliano_Calvo)\
**Post date:** [17 May 2007 12:27 UTC](https://rubytalk.org/t/coopting-string-interpolation/37654/8 "2007-05-17T12:27:56Z")

</div>

> \> I'm trying to do some metaprogramming and I need to apply some  
> \> operation to all the strings that are interpolated. But I can't change  
> \> the interpolation. In the example, I would like that "hi, #{name}" to  
> \> evaluate to "hi, AURE" instead of "hi, Aure". I know that inside the  
> \> #{} "operator" I can put any ruby code, so "hi, #{name.capitalize}"  
> \> would evaluate to what I want.  
> \>  
> \> But I want it to execute code that I DON'T write there. Ideally, there  
> \> should be a method hook or something to change the way the  
> \> interpolation works. But I couldn't find it :(. May be a different  
> \> example may be more clear. How can I do to write to a file all the  
> \> strings generated via interpolations (id est, all the strings that are  
> \> generated evaluating the different #{} "operators" in a program)?
> 
> Frankly, it has not become clear to me what you are up to. Can you  
> maybe just state which problem you are trying to solve?

NDAs are a bitch. I can't state the exact problem (bah! I can, but I  
could be fired, in a trial, and I also might inhibit my coworkers to  
publish a paper with their findings). I'm working in a research  
project and I need to do some "clever stuff" (that's the thing I can't  
disclose) to all the Strings. This thing is "different" depending on  
how strings are composed. All the ways I know for string composition  
but string interpolation (\<\<, +, concat, gsub, etc.) can be overridden  
redefining methods in the string class. I'm looking for a way to  
intercept the string expansion to do my thing. Does ruby internally  
call some overridable method to compose the strings used in a  
interpolation?

Now I see that if I can transform all the \<"a#{expression}c"\> in \<"a"  
+ (expression) + "c"\> in a ruby source code string, I could change  
expression with "a#{expression}c" in "a" + clever\_stuff(expression) +  
"c" and use my changed + method in Strings. Is there an easy way to  
manipulate ruby code in ruby to do this?

Thanks for your time,  
Aureliano.

---

<div class="post-metadata">

**Author:** ![Mariusz\_Pekala](https://avatars.discourse-cdn.com/v4/letter/m/7ba0ec/32.png) [@Mariusz\_Pekala](https://rubytalk.org/u/Mariusz_Pekala)\
**Post date:** [17 May 2007 14:39 UTC](https://rubytalk.org/t/coopting-string-interpolation/37654/9 "2007-05-17T14:39:47Z")

</div>

Maybe this will be of some use:

irb(main):005:0\> "asd\_#{raise caller.to\_s}\_"  
RuntimeError: /usr/lib/ruby/1.8/irb/workspace.rb:52:in `irb\_binding'/usr/lib/ruby/1.8/irb/workspace.rb:52  
&nbsp;&nbsp;&nbsp;&nbsp;from (irb):5

> **···**
>
> On 2007-05-17 21:27:56 +0900 (Thu, May), Aureliano Calvo wrote:
> 
> > Now I see that if I can transform all the \<"a#{expression}c"\> in \<"a"  
> > + (expression) + "c"\> in a ruby source code string, I could change  
> > expression with "a#{expression}c" in "a" + clever\_stuff(expression) +  
> > "c" and use my changed + method in Strings. Is there an easy way to  
> > manipulate ruby code in ruby to do this?
> 
> --  
> No virus found in this outgoing message.  
> Checked by 'grep -i virus $MESSAGE'  
> Trust me.

---

<div class="post-metadata">

**Author:** ![Gavin\_Kistner2](https://avatars.discourse-cdn.com/v4/letter/g/b9e5f3/32.png) [@Gavin\_Kistner2](https://rubytalk.org/u/Gavin_Kistner2)\
**Post date:** [17 May 2007 17:05 UTC](https://rubytalk.org/t/coopting-string-interpolation/37654/10 "2007-05-17T17:05:07Z")

</div>

Can you make life easier by changing to use ERB?  
You cannot change Ruby string interpolation in Ruby. You can hack the  
source code if you really need to. You can manually change your  
strings, or you can (as below) use gsub to change the string before  
evaluating.

&nbsp;&nbsp;def clever\_stuff( str )  
&nbsp;&nbsp;&nbsp;&nbsp;str.upcase  
&nbsp;&nbsp;end

&nbsp;&nbsp;require 'erb'  
&nbsp;&nbsp;def clever\_template( str )  
&nbsp;&nbsp;&nbsp;&nbsp;ERB.new( str.gsub( /\<%=(.+?)%\>/, '\<  
%=clever\_stuff(\1)%\>' ) ).result  
&nbsp;&nbsp;end

&nbsp;&nbsp;name1, name2 = %w|Gavin Kistner|

&nbsp;&nbsp;str1 = "Hello there, \<%=name1%\> \<%=name2%\>! Nice to meet you!"  
&nbsp;&nbsp;puts clever\_template( str1 )  
&nbsp;&nbsp;#=\> Hello there, GAVIN KISTNER! Nice to meet you!

&nbsp;&nbsp;str2 = "Hello there, \<%=name1 + ' ' + name2%\>! Nice to meet you!"  
&nbsp;&nbsp;puts clever\_template( str2 )  
&nbsp;&nbsp;#=\> Hello there, GAVIN KISTNER! Nice to meet you!

> **···**
>
> On May 17, 6:27 am, "Aureliano Calvo" \<aurelianoca...@gmail.com\> wrote:
> 
> > Now I see that if I can transform all the \<"a#{expression}c"\> in \<"a"  
> > + (expression) + "c"\> in a ruby source code string, I could change  
> > expression with "a#{expression}c" in "a" + clever\_stuff(expression) +  
> > "c" and use my changed + method in Strings. Is there an easy way to  
> > manipulate ruby code in ruby to do this?

---

<div class="post-metadata">

**Author:** ![Pit](https://avatars.discourse-cdn.com/v4/letter/p/f14d63/32.png) [@Pit](https://rubytalk.org/u/Pit)\
**Post date:** [17 May 2007 18:49 UTC](https://rubytalk.org/t/coopting-string-interpolation/37654/11 "2007-05-17T18:49:12Z")

</div>

Aureliano Calvo schrieb:

> Now I see that if I can transform all the \<"a#{expression}c"\> in \<"a"  
> + (expression) + "c"\> in a ruby source code string, I could change  
> expression with "a#{expression}c" in "a" + clever\_stuff(expression) +  
> "c" and use my changed + method in Strings. Is there an easy way to  
> manipulate ruby code in ruby to do this?

Aureliano, is it right that you don't need to manipulate the code at runtime, and you can work with the source code instead? This can be more or less hard to do, depending on the complexity of the expressions which are allowed inside the strings. For simple expressions a regex solution might be enough. Otherwise you'd need to use one of the Ruby parser libraries.

If this is for a research project, then maybe you could patch the Ruby interpreter to add the hook you need. This shouldn't be too difficult.

Regards,  
Pit

---

<div class="post-metadata">

**Author:** ![Daniel\_DeLorme](https://avatars.discourse-cdn.com/v4/letter/d/9d8465/32.png) [@Daniel\_DeLorme](https://rubytalk.org/u/Daniel_DeLorme)\
**Post date:** [17 May 2007 22:53 UTC](https://rubytalk.org/t/coopting-string-interpolation/37654/12 "2007-05-17T22:53:31Z")

</div>

Aureliano Calvo wrote:

> Now I see that if I can transform all the \<"a#{expression}c"\> in \<"a"  
> + (expression) + "c"\> in a ruby source code string, I could change  
> expression with "a#{expression}c" in "a" + clever\_stuff(expression) +  
> "c" and use my changed + method in Strings. Is there an easy way to  
> manipulate ruby code in ruby to do this?

Maybe you can take a look at ParseTree to decompose a dynamic string into its components:

&nbsp;&nbsp;require "rubygems"  
&nbsp;&nbsp;require "parse\_tree"  
&nbsp;&nbsp;class Foo  
&nbsp;&nbsp;&nbsp;&nbsp;def foo  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"interpolation: #{42}!"  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
&nbsp;&nbsp;ParseTree.new.parse\_tree(Foo)  
=\> [[:class, :Foo, [:const, :Object], [:defn, :foo, [:scope, [:block, [:args], [:dstr, "interpolation: ", [:lit, 42], [:str, "!"]]]]]]]

Daniel

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [18 May 2007 08:05 UTC](https://rubytalk.org/t/coopting-string-interpolation/37654/13 "2007-05-18T08:05:07Z")

</div>

> > \> I'm trying to do some metaprogramming and I need to apply some  
> > \> operation to all the strings that are interpolated. But I can't change  
> > \> the interpolation. In the example, I would like that "hi, #{name}" to  
> > \> evaluate to "hi, AURE" instead of "hi, Aure". I know that inside the  
> > \> #{} "operator" I can put any ruby code, so "hi, #{name.capitalize}"  
> > \> would evaluate to what I want.  
> > \>  
> > \> But I want it to execute code that I DON'T write there. Ideally, there  
> > \> should be a method hook or something to change the way the  
> > \> interpolation works. But I couldn't find it :(. May be a different  
> > \> example may be more clear. How can I do to write to a file all the  
> > \> strings generated via interpolations (id est, all the strings that are  
> > \> generated evaluating the different #{} "operators" in a program)?
> > 
> > Frankly, it has not become clear to me what you are up to. Can you  
> > maybe just state which problem you are trying to solve?
> 
> NDAs are a bitch. I can't state the exact problem (bah! I can, but I  
> could be fired, in a trial, and I also might inhibit my coworkers to  
> publish a paper with their findings).

You can tell us - we won't tell anybody else. 😉

> I'm working in a research  
> project and I need to do some "clever stuff" (that's the thing I can't  
> disclose) to all the Strings. This thing is "different" depending on  
> how strings are composed. All the ways I know for string composition  
> but string interpolation (\<\<, +, concat, gsub, etc.) can be overridden  
> redefining methods in the string class. I'm looking for a way to  
> intercept the string expansion to do my thing. Does ruby internally  
> call some overridable method to compose the strings used in a  
> interpolation?

Likely but also likely not accessible to pure Ruby code. But in your case (research project) it might be ok to hack the interpreter if you need to catch all string interpolations. Did you look into this yet?

> Now I see that if I can transform all the \<"a#{expression}c"\> in \<"a"  
> + (expression) + "c"\> in a ruby source code string, I could change  
> expression with "a#{expression}c" in "a" + clever\_stuff(expression) +  
> "c" and use my changed + method in Strings. Is there an easy way to  
> manipulate ruby code in ruby to do this?

You could even change all "a#{b}c" into "a#{your\_magic b}c" which is considerably easier to do than a complete parse. But when I think about it it may break for some b's. Whether that's ok or not, I don't know.

Kind regards

&nbsp;&nbsp;robert

> **···**
>
> On 17.05.2007 14:27, Aureliano Calvo wrote:

---

<div class="post-metadata">

**Author:** ![Mariusz\_Pekala](https://avatars.discourse-cdn.com/v4/letter/m/7ba0ec/32.png) [@Mariusz\_Pekala](https://rubytalk.org/u/Mariusz_Pekala)\
**Post date:** [17 May 2007 14:51 UTC](https://rubytalk.org/t/coopting-string-interpolation/37654/14 "2007-05-17T14:51:24Z")

</div>

Oh, no - it's stupid. Sorry 😉

> **···**
>
> > On 2007-05-17 21:27:56 +0900 (Thu, May), Aureliano Calvo wrote:  
> > \> Now I see that if I can transform all the \<"a#{expression}c"\> in \<"a"  
> > \> + (expression) + "c"\> in a ruby source code string, I could change  
> > \> expression with "a#{expression}c" in "a" + clever\_stuff(expression) +  
> > \> "c" and use my changed + method in Strings. Is there an easy way to  
> > \> manipulate ruby code in ruby to do this?
> > 
> > Maybe this will be of some use:
> > 
> > irb(main):005:0\> "asd\_#{raise caller.to\_s}\_"  
> > RuntimeError: /usr/lib/ruby/1.8/irb/workspace.rb:52:in `irb\_binding'/usr/lib/ruby/1.8/irb/workspace.rb:52  
> > &nbsp;&nbsp;&nbsp;&nbsp;from (irb):5
> 
> --  
> No virus found in this outgoing message.  
> Checked by 'grep -i virus $MESSAGE'  
> Trust me.

---

<div class="post-metadata">

**Author:** ![Aureliano\_Calvo](https://avatars.discourse-cdn.com/v4/letter/a/f04885/32.png) [@Aureliano\_Calvo](https://rubytalk.org/u/Aureliano_Calvo)\
**Post date:** [17 May 2007 23:47 UTC](https://rubytalk.org/t/coopting-string-interpolation/37654/15 "2007-05-17T23:47:13Z")

</div>

> \> Now I see that if I can transform all the \<"a#{expression}c"\> in \<"a"  
> \> + (expression) + "c"\> in a ruby source code string, I could change  
> \> expression with "a#{expression}c" in "a" + clever\_stuff(expression) +  
> \> "c" and use my changed + method in Strings. Is there an easy way to  
> \> manipulate ruby code in ruby to do this?
> 
> Maybe you can take a look at ParseTree to decompose a dynamic string  
> into its components:
> 
> &nbsp;&nbsp;require "rubygems"  
> &nbsp;&nbsp;require "parse\_tree"  
> &nbsp;&nbsp;class Foo  
> &nbsp;&nbsp;&nbsp;&nbsp;def foo  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"interpolation: #{42}!"  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> &nbsp;&nbsp;ParseTree.new.parse\_tree(Foo)  
> =\> [[:class, :Foo, [:const, :Object], [:defn, :foo, [:scope, [:block,  
> [:args], [:dstr, "interpolation: ", [:lit, 42], [:str, "!"]]]]]]]

Thanks!  
I'll take a look at it.

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [18 May 2007 12:49 UTC](https://rubytalk.org/t/coopting-string-interpolation/37654/16 "2007-05-18T12:49:58Z")

</div>

Here's a simpler idea. Given that string interpolation calls to\_s, you can  
use this as your hook if you wrap your objects in a proxy object. You need  
to ensure that operations like '+' also return an instance of this proxy  
object, so you can do all the operations you want whilst keeping the string  
wrapped. Then the only case that to\_s is called is when the interpolation  
takes place, which gives you the hook you're looking for (as long as you're  
not calling to\_s in any other context)

Example:

class WrapString  
&nbsp;&nbsp;def initialize(str)  
&nbsp;&nbsp;&nbsp;&nbsp;@str = str  
&nbsp;&nbsp;end

&nbsp;&nbsp;def str  
&nbsp;&nbsp;&nbsp;&nbsp;@str  
&nbsp;&nbsp;end  
&nbsp;&nbsp;protected :str

&nbsp;&nbsp;def +(other)  
&nbsp;&nbsp;&nbsp;&nbsp;self.class.new(str + (other.str rescue other.to\_s))  
&nbsp;&nbsp;end  
&nbsp;&nbsp;# rinse and repeat

&nbsp;&nbsp;# Here is your interpolation hook:  
&nbsp;&nbsp;def to\_s  
&nbsp;&nbsp;&nbsp;&nbsp;str.to\_s \* 2  
&nbsp;&nbsp;end  
end

a = WrapString.new("abc")  
b = WrapString.new("def")

puts "Answer is #{a + b}"

a = WrapString.new("abc")  
b = "def"

puts "Answer is #{a + b}"

The result of a + b is \<WrapString @str="abcdef"\>, and then the  
interpolation calls to\_s which in this case just doubles it to  
"abcdefabcdef"

There's probably a neater implementation of the above using method\_missing  
or a delegation pattern rather than enumerating all the methods of String,  
but you get the idea.

Just a thought?

Brian.

> **···**
>
> On Fri, May 18, 2007 at 05:05:07PM +0900, Robert Klemme wrote:
> 
> > \>I'm working in a research  
> > \>project and I need to do some "clever stuff" (that's the thing I can't  
> > \>disclose) to all the Strings. This thing is "different" depending on  
> > \>how strings are composed. All the ways I know for string composition  
> > \>but string interpolation (\<\<, +, concat, gsub, etc.) can be overridden  
> > \>redefining methods in the string class. I'm looking for a way to  
> > \>intercept the string expansion to do my thing. Does ruby internally  
> > \>call some overridable method to compose the strings used in a  
> > \>interpolation?
> > 
> > Likely but also likely not accessible to pure Ruby code. But in your  
> > case (research project) it might be ok to hack the interpreter if you  
> > need to catch all string interpolations. Did you look into this yet?

---

<div class="post-metadata">

**Author:** ![Aureliano\_Calvo](https://avatars.discourse-cdn.com/v4/letter/a/f04885/32.png) [@Aureliano\_Calvo](https://rubytalk.org/u/Aureliano_Calvo)\
**Post date:** [19 May 2007 03:46 UTC](https://rubytalk.org/t/coopting-string-interpolation/37654/17 "2007-05-19T03:46:02Z")

</div>

> \> I'm working in a research  
> \> project and I need to do some "clever stuff" (that's the thing I can't  
> \> disclose) to all the Strings. This thing is "different" depending on  
> \> how strings are composed. All the ways I know for string composition  
> \> but string interpolation (\<\<, +, concat, gsub, etc.) can be overridden  
> \> redefining methods in the string class. I'm looking for a way to  
> \> intercept the string expansion to do my thing. Does ruby internally  
> \> call some overridable method to compose the strings used in a  
> \> interpolation?
> 
> Likely but also likely not accessible to pure Ruby code. But in your  
> case (research project) it might be ok to hack the interpreter if you  
> need to catch all string interpolations. Did you look into this yet?

I'll see if I can get away with it. I'll provide a patch to have an  
interpolation hook if I manage to implement it.

Thanks,  
Aureliano.

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [18 May 2007 13:55 UTC](https://rubytalk.org/t/coopting-string-interpolation/37654/18 "2007-05-18T13:55:06Z")

</div>

Not bad! Question is: what does it do to the rest of the code? Just think about using obj.to\_s for Hash keys. Also, I am not sure what happens in core code if to\_s returns something other than a string:

15:51:07 [~]: irb  
irb(main):001:0\> o=Object.new  
=\> #\<Object:0x7ef9e3f8\>  
irb(main):002:0\> puts o  
#\<Object:0x7ef9e3f8\>  
=\> nil  
irb(main):003:0\> def o.to\_s() "foo" end  
=\> nil  
irb(main):004:0\> puts o  
foo  
=\> nil  
irb(main):005:0\> def o.to\_s() 123 end  
=\> nil  
irb(main):006:0\> puts o  
#\<Object:0x7ef9e3f8\>  
=\> nil

Seems to be there could be wired effects...

Kind regards

&nbsp;&nbsp;robert

> **···**
>
> On 18.05.2007 14:49, Brian Candler wrote:
> 
> > On Fri, May 18, 2007 at 05:05:07PM +0900, Robert Klemme wrote:
> > 
> > > > I'm working in a research  
> > > > project and I need to do some "clever stuff" (that's the thing I can't  
> > > > disclose) to all the Strings. This thing is "different" depending on  
> > > > how strings are composed. All the ways I know for string composition  
> > > > but string interpolation (\<\<, +, concat, gsub, etc.) can be overridden  
> > > > redefining methods in the string class. I'm looking for a way to  
> > > > intercept the string expansion to do my thing. Does ruby internally  
> > > > call some overridable method to compose the strings used in a  
> > > > interpolation?
> > > 
> > > Likely but also likely not accessible to pure Ruby code. But in your case (research project) it might be ok to hack the interpreter if you need to catch all string interpolations. Did you look into this yet?
> > 
> > Here's a simpler idea. Given that string interpolation calls to\_s, you can  
> > use this as your hook if you wrap your objects in a proxy object. You need  
> > to ensure that operations like '+' also return an instance of this proxy  
> > object, so you can do all the operations you want whilst keeping the string  
> > wrapped. Then the only case that to\_s is called is when the interpolation  
> > takes place, which gives you the hook you're looking for (as long as you're  
> > not calling to\_s in any other context)
> > 
> > Example:
> > 
> > class WrapString  
> > &nbsp;&nbsp;def initialize(str)  
> > &nbsp;&nbsp;&nbsp;&nbsp;@str = str  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;def str  
> > &nbsp;&nbsp;&nbsp;&nbsp;@str  
> > &nbsp;&nbsp;end  
> > &nbsp;&nbsp;protected :str
> > 
> > &nbsp;&nbsp;def +(other)  
> > &nbsp;&nbsp;&nbsp;&nbsp;self.class.new(str + (other.str rescue other.to\_s))  
> > &nbsp;&nbsp;end  
> > &nbsp;&nbsp;# rinse and repeat
> > 
> > &nbsp;&nbsp;# Here is your interpolation hook:  
> > &nbsp;&nbsp;def to\_s  
> > &nbsp;&nbsp;&nbsp;&nbsp;str.to\_s \* 2  
> > &nbsp;&nbsp;end  
> > end
> > 
> > a = WrapString.new("abc")  
> > b = WrapString.new("def")
> > 
> > puts "Answer is #{a + b}"
> > 
> > a = WrapString.new("abc")  
> > b = "def"
> > 
> > puts "Answer is #{a + b}"
> > 
> > The result of a + b is \<WrapString @str="abcdef"\>, and then the  
> > interpolation calls to\_s which in this case just doubles it to  
> > "abcdefabcdef"
> > 
> > There's probably a neater implementation of the above using method\_missing  
> > or a delegation pattern rather than enumerating all the methods of String,  
> > but you get the idea.
> > 
> > Just a thought?
