# Bug? String interpolation and continuations

**URL:** <https://rubytalk.org/t/bug-string-interpolation-and-continuations/12708>\
**Category:** ruby-talk\
**Created:** [22 July 2004 06:52 UTC](https://rubytalk.org/t/bug-string-interpolation-and-continuations/12708 "2004-07-22T06:52:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sam\_McCall](https://avatars.discourse-cdn.com/v4/letter/s/e56c9b/32.png) [@Sam\_McCall](https://rubytalk.org/u/Sam_McCall)\
**Post date:** [22 July 2004 06:52 UTC](https://rubytalk.org/t/bug-string-interpolation-and-continuations/12708/1 "2004-07-22T06:52:01Z")

</div>

The following gives me weird results:  
(Typed into IRB. The results are similar in ruby but because of the simple example you get an infinite loop)

> **···**
>
> =================================  
> # Allow a continuation to escape  
> store\_cont = proc {|cont| $cont=cont; nil}
> 
> puts "[#{callcc &store\_cont}]"  
> # prints '[]', as expected
> 
> $cont.call("hello")  
> # prints '[]hello]'  
> # expected '[hello]'
> 
> # $cont.call("world") # prints '[]hello]world]' # expected '[world]'
> 
> I tested this with "ruby 1.8.2 (2004-07-16)".
> 
> Sam

---

<div class="post-metadata">

**Author:** ![Carlos](https://avatars.discourse-cdn.com/v4/letter/c/ecae2f/32.png) [@Carlos](https://rubytalk.org/u/Carlos)\
**Post date:** [22 July 2004 10:20 UTC](https://rubytalk.org/t/bug-string-interpolation-and-continuations/12708/2 "2004-07-22T10:20:46Z")

</div>

[Sam McCall \<tunah.usenet@tunah.net\>, 2004-07-22 08.52 CEST]

> The following gives me weird results:  
> (Typed into IRB. The results are similar in ruby but because of the  
> simple example you get an infinite loop)
> 
> =================================  
> # Allow a continuation to escape  
> store\_cont = proc {|cont| $cont=cont; nil}
> 
> puts "[#{callcc &store\_cont}]"  
> # prints '', as expected
> 
> $cont.call("hello")  
> # prints 'hello]'  
> # expected '[hello]'

My guess is, the line with the puts and the interpolated string is  
internally translated to something like:

s = "["  
s \<\< callcc &store\_cont  
s \<\< "]"

puts s

> **···**
>
> --

---

<div class="post-metadata">

**Author:** ![Yukihiro\_Matsumoto2](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yukihiro\_Matsumoto2](https://rubytalk.org/u/Yukihiro_Matsumoto2)\
**Post date:** [24 July 2004 06:01 UTC](https://rubytalk.org/t/bug-string-interpolation-and-continuations/12708/3 "2004-07-24T06:01:10Z")

</div>

Hi,

> The following gives me weird results:  
> (Typed into IRB. The results are similar in ruby but because of the  
> simple example you get an infinite loop)

It's not a bug. It's a feature.

> # Allow a continuation to escape  
> store\_cont = proc {|cont| $cont=cont; nil}
> 
> puts "[#{callcc &store\_cont}]"  
> # prints '', as expected

saves the continuation to $cont, then evaluate "[#{nil}]".

> $cont.call("hello")  
> # prints 'hello]hello]hello]...'  
> # expected '[hello]'

By calling the continuation the execution goes back to the saved  
point, but no other states are restored, so that the value of  
generating string (referenced from the local variable) remains the  
last generated value after the jump. Then execution concatenate  
"hello", then "]". Repeat forever.

That's what happened.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;matz.

> **···**
>
> In message "Bug? String interpolation and continuations" on 04/07/22, Sam McCall \<tunah.usenet@tunah.net\> writes:

---

<div class="post-metadata">

**Author:** ![Gltewalt](https://avatars.discourse-cdn.com/v4/letter/g/c89c15/32.png) [@Gltewalt](https://rubytalk.org/u/Gltewalt)\
**Post date:** [22 July 2004 21:07 UTC](https://rubytalk.org/t/bug-string-interpolation-and-continuations/12708/4 "2004-07-22T21:07:00Z")

</div>

Carlos \<angus@quovadis.com.ar\> wrote in message news:\<20040722102042.GA1575@quovadis.com.ar\>...

> [Sam McCall \<tunah.usenet@tunah.net\>, 2004-07-22 08.52 CEST]  
> \> The following gives me weird results:  
> \> (Typed into IRB. The results are similar in ruby but because of the  
> \> simple example you get an infinite loop)  
> \>  
> \> =================================  
> \> # Allow a continuation to escape  
> \> store\_cont = proc {|cont| $cont=cont; nil}  
> \>  
> \> puts "[#{callcc &store\_cont}]"  
> \> # prints '', as expected  
> \>  
> \> $cont.call("hello")  
> \> # prints 'hello]'  
> \> # expected '[hello]'
> 
> My guess is, the line with the puts and the interpolated string is  
> internally translated to something like:
> 
> s = "["  
> s \<\< callcc &store\_cont  
> s \<\< "]"
> 
> puts s
> 
> --

This doesnt address your problem fully, but you can do:  
puts "[#{callcc &store\_cont}" + "]"  
$cont.call("hello")  
# [hello]  
# =\> nil

---

<div class="post-metadata">

**Author:** ![Sam\_McCall](https://avatars.discourse-cdn.com/v4/letter/s/e56c9b/32.png) [@Sam\_McCall](https://rubytalk.org/u/Sam_McCall)\
**Post date:** [24 July 2004 11:51 UTC](https://rubytalk.org/t/bug-string-interpolation-and-continuations/12708/5 "2004-07-24T11:51:57Z")

</div>

Yukihiro Matsumoto wrote:

> Hi,
> 
> \>The following gives me weird results:  
> \>(Typed into IRB. The results are similar in ruby but because of the \>simple example you get an infinite loop)
> 
> It's not a bug. It's a feature.

Just to be clear, I did expect the infinite loop 🙂 But I assumed that it would reinterpolate the string rather than continue appending.

> \>$cont.call("hello")  
> \># prints 'hello]hello]hello]...'  
> \># expected '[hello]'
> 
> By calling the continuation the execution goes back to the saved  
> point, but no other states are restored, so that the value of  
> generating string (referenced from the local variable) remains the  
> last generated value after the jump. Then execution concatenate  
> "hello", then "]". Repeat forever.
> 
> That's what happened.

Thanks, that makes sense, although it is surprising until I think about implementation details. (I haven't read the spec yet... not a native C speaker 😉

I expected  
s="#{foo()}-#{callcc &store\_cont}-#{bar()}"  
to have the interpolated values evaluated first (left to right), and then have the string assembled afterwards (by analogy with method calls having arguments evaluated first). foo() would be called once, but bar() would be called every time the continuation was resumed.

On the other hand even if this was better behaviour I guess it isn't work the performance costs, just for continuations.

Sam

> **···**
>
> > In message "Bug? String interpolation and continuations" \> on 04/07/22, Sam McCall \<tunah.usenet@tunah.net\> writes:

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [23 July 2004 09:46 UTC](https://rubytalk.org/t/bug-string-interpolation-and-continuations/12708/6 "2004-07-23T09:46:34Z")

</div>

> This doesnt address your problem fully, but you can do:  
> puts "[#{callcc &store\_cont}" + "]"

Well, this is probably this that he want

&nbsp;&nbsp;puts "["+ callcc(&store\_cont) + "]"

but, perhaps, the best is to have the real example

Guy Decoux

---

<div class="post-metadata">

**Author:** ![Sam\_McCall](https://avatars.discourse-cdn.com/v4/letter/s/e56c9b/32.png) [@Sam\_McCall](https://rubytalk.org/u/Sam_McCall)\
**Post date:** [24 July 2004 04:46 UTC](https://rubytalk.org/t/bug-string-interpolation-and-continuations/12708/7 "2004-07-24T04:46:56Z")

</div>

> This doesnt address your problem fully, but you can do:  
> puts "[#{callcc &store\_cont}" + "]"

Yes 🙂

> Well, this is probably this that he want
> 
> &nbsp;&nbsp;puts "["+ callcc(&store\_cont) + "]"
> 
> but, perhaps, the best is to have the real example

Actually, I was just playing around with continuations when I hit this. Obviously it's easily worked around and it's never going to be a major problem, but I thought I should report it.

Sam
