Forum: Ruby String interpolation at a later stage

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
934f858e451cf9b771996b2940cd696b?d=identicon&s=25 Raj Singh (dorelal)
on 2009-01-16 16:43
x = 'hello'
y = '#{x} world'

desired result = 'hello world'


Please note that variable y has the string interpolation code under
single quotes.

I have these two values x and y. And with that I need to get to the
desired result. Any suggestion on how to do that.

I tried eval y but that won't work because world is not a variable.
8853f712852b03ba6646b59b1723f44d?d=identicon&s=25 Flower Born (flowerborn)
on 2009-01-16 16:56
(Received via mailing list)
* Raj Singh <neeraj.jsr@gmail.com> [2009-01-17 00:41:54 +0900]:

> desired result. Any suggestion on how to do that.
>
> I tried eval y but that won't work because world is not a variable.

eval %Q["#{y}"]

Jan
23172b6630dc631a134c9bad2fec2a39?d=identicon&s=25 Chris Hulan (Guest)
on 2009-01-16 18:45
(Received via mailing list)
On Jan 16, 10:41 am, Raj Singh <neeraj....@gmail.com> wrote:
>
> I tried eval y but that won't work because world is not a variable.
> --
> Posted viahttp://www.ruby-forum.com/.

try:
z = '"' + y + '"'
zz = eval z
E7559e558ececa67c40f452483b9ac8c?d=identicon&s=25 Gary Wright (Guest)
on 2009-01-16 19:25
(Received via mailing list)
On Jan 16, 2009, at 10:41 AM, Raj Singh wrote:

> x = 'hello'
> y = '#{x} world'
>
> desired result = 'hello world'

This might not be exactly what you were asking for but might be
helpful none the less:

x = 'hello'
lazy = lambda {"#{x} world"}

result = lazy.call
result = lazy[]

# or maybe even..

def lazy.to_s
   call
end

puts lazy        # hello world

x = 'goodbye'

puts lazy        # goodbye world
This topic is locked and can not be replied to.