Clean way to build long strings in Ruby

时光怂恿深爱的人放手 提交于 2019-12-12 10:59:21

问题


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.


回答1:


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



回答2:


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.




回答3:


Since Ruby 2.3 you have the squiggly heredoc to avoid indentations in the string and still keep indentations in your code.

See here for more info.

Here the example from that page.

class Subscription
  def warning_message
    <<~HEREDOC
      Subscription expiring soon!
      Your free trial will expire in #{days_until_expiration} days.
      Please update your billing information.
    HEREDOC
  end
end

If you don't mind the indentations you can also use the %Q{} syntax like this, the %Q gives string substitution, the %q not.

warning_message = %Q{
  Subscription expiring soon!
  Your free trial will expire in #{days_until_expiration} days.
  Please update your billing information.
}


来源:https://stackoverflow.com/questions/13359746/clean-way-to-build-long-strings-in-ruby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!