String interpolation at a later stage

R

Raj Singh

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

Chris Hulan

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.

try:
z = '"' + y + '"'
zz = eval z
 
G

Gary Wright

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,120
Latest member
ShelaWalli
Top