0

I was wondering if there was a way to setup a string with interpolation to have the code in the string run multiple times. (I know I could just put the code in a loop, but for the sake of wondering).

For example, I would like the following code:

x = "#{puts 'a'}"

3.times do
   x
end

To have the output:

a
a
a

This issue I've found is that interpolation is resolved upon initialization. But is there a way to setup a string so that the interpolation can be reused?

1 Answer 1

1

String interpolations are not made for this purpose. Strings are supposed to express semantics, not behaviours. Don't call puts inside an interpolated string.

You can still do something like:

def x
  puts 'a'
end

3.times do
  x
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.