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}"