Breaking Up Long Strings On Multiple Lines In Ruby Without Stripping Newlines


Answer :

Maybe this is what you're looking for?

string = "line #1"\          "line #2"\          "line #3"  p string # => "line #1line #2line #3" 

You can use \ to indicate that any line of Ruby continues on the next line. This works with strings too:

string = "this is a \ string that spans lines"  puts string.inspect 

will output "this is a string that spans lines"


Three years later, there is now a solution in Ruby 2.3: The squiggly heredoc.

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 

Blog post link: https://infinum.co/the-capsized-eight/articles/multiline-strings-ruby-2-3-0-the-squiggly-heredoc

The indentation of the least-indented line will be removed from each line of the content.


Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android How Can I Convert A String To A Editable