“...I've been working since 2008 with Ruby / Ruby on Rails, love a bit of Elixir / Phoenix and learning Rust. I also poke through other people's code and make PRs for OpenSource Ruby projects that sometimes make it. Currently working for InPay...”

Rob Lacey (contact@robl.me)
Senior Software Engineer, Brighton, UK

Multiline Ruby String without interpolation

Whilst trying to clean up old blog posts. I thought I’d just re-assign the whole post on the console. However, the content of the post had code examples and these examples were being interpolated. This makes sense but isn’t what I wanted. These are all (nearly) equivalent other than the new lines.

s =<<-STR
#{Time.now}
STR
# => "2017-01-17 06:43:48 -0500" 

s = %(
#{Time.now}
)
# => "\n2017-01-17 06:43:48 -0500\n"

s = %Q(
#{Time.now}
)
# => "\n2017-01-17 06:43:48 -0500\n"

But what I really want it multi-line string assignment without interpolation.

s = %q(
#{Time.now}
)
# => "\n\#{Time.now}\n"

And without the new lines.

s = %q(
#{Time.now}
).lstrip.chop
# => "\#{Time.now}"