Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clean way to build long strings in Ruby

When writing Ruby (client scripts) I see three ways to build longer strings, including line-endings, all of which "smell" kind of ugly to me.

Are there any cleaner and nicer ways?

The variable-incrementing.

if render_quote?
  quote =  "Now that there is the Tec-9, a crappy spray gun from South Miami."
  quote += "This gun is advertised as the most popular gun in American crime. Do you believe that shit?"
  quote += "It actually says that in the little book that comes with it: the most popular gun in American crime."
  quote += "Like they're actually proud of that shit."
  puts quote
end

Heredocs (and unclosed quotes).

if render_quote?
  quote =<<EOS
Now that there is the Tec-9, a crappy spray gun from South Miami.
This gun is advertised as the most popular gun in American crime. Do you believe that shit?
It actually says that in the little book that comes with it: the most popular gun in American crime.
Like they're actually proud of that shit.
EOS

  puts quote
end

Or, by simply not adding a closing tag:

if render_quote?
  quote = "Now that there is the Tec-9, a crappy spray gun from South Miami.
This gun is advertised as the most popular gun in American crime. Do you believe that shit?
It actually says that in the little book that comes with it: the most popular gun in American crime.
Like they're actually proud of that shit."

  puts quote
end

Or, optionally, with a gsub to fix the identation-issues (yuk!?).

Concatenating.

if render_quote?
  quote =  "Now that there is the Tec-9, a crappy spray gun from South Miami."
  quote += "This gun is advertised as the most popular gun in American crime. Do you believe that shit?"
  quote += "It actually says that in the little book that comes with it: the most popular gun in American crime."
  quote += "Like they're actually proud of that shit."
  puts quote
end

( quote from Samuel L. Ipsum )

I am aware that having such strings (i.e. view-logic) trough my scripts is a smell in itself, but don't know of a pattern (other then po-files or so) to clean this up.

like image 972
berkes Avatar asked Nov 13 '12 11:11

berkes


People also ask

What is string interpolation in Ruby?

String Interpolation, it is all about combining strings together, but not by using the + operator. String Interpolation works only when we use double quotes (“”) for the string formation. String Interpolation provides an easy way to process String literals.


2 Answers

Note that adjacent string literals are concatenated. You can combine this with line-continuing character \.

if render_quote?
  quote =
  "Now that there is the Tec-9, a crappy spray gun from South Miami. " \
  "This gun is advertised as the most popular gun in American crime. " \
  "Do you believe that shit?" \
  "It actually says that in the little book that comes with it: " \
  "the most popular gun in American crime. " \
  "Like they're actually proud of that shit."
  puts quote
end
like image 67
sawa Avatar answered Sep 20 '22 07:09

sawa


Your code wasn't working for me with the dash afterwards... but this works, doesn't require additional escaping of new lines, and simply says what it is doing on the HereDoc.

if render_quote?
  quote = <<-EOS.strip.split.join(' ')
    Now that there is the Tec-9, a crappy spray gun from South Miami.
    This gun is advertised as the most popular gun in American crime. Do you believe that shit?
    It actually says that in the little book that comes with it: the most popular gun in American crime.
    Like they're actually proud of that shit.
  EOS

  puts quote
end

The dash before the EOS signifies that I will be able to use the EOS in an indented manner.

like image 44
vgoff Avatar answered Sep 20 '22 07:09

vgoff