rubystring-interpolation

Interpolating Ruby code within quotes within a string


I have a string like this:

'say "Twenty-five minutes remaining."'

How would I interpolate more dynamic content into that message? I already know the approach below doesn't work, as the content is returned literally.

'say "#{minutes_remaining} minutes remaining."'

Solution

  • This works:

    minutes_remaining = 25
    system("say '#{minutes_remaining} minutes remaining.'")
    

    String interpolation only works with double quotes, not single quotes.