A clean way to create long lines in Ruby

When writing Ruby (client scripts), I see three ways to create longer lines, including line endings, all of which "smell" me somehow ugly.

Are there any cleaner and nicer ways?

Variable increment.

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 closed quotation marks).

 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 simply without 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 gsub to fix the -issues identifier (yuk !?).

concatenation.

 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 know that the presence of such lines (i.e. view logic) through my scripts is a smell in itself, but I don’t know which template (others, i.e. po files or so) to clear it.

+4
source share
2 answers

Note that adjacent string literals are concatenated. You can combine this with the line continuation 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 
+4
source

After that, your code did not work for me with a dash ... but it works, does not require additional screening of new lines and just says what it does on 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 EOS means that I can use EOS indented.

+1
source

Source: https://habr.com/ru/post/1445745/


All Articles