Breaking up long strings on multiple lines in Ruby without stripping newlines

后端 未结 5 1982
心在旅途
心在旅途 2020-12-12 09:37

We recently decided at my job to a ruby style guide. One of the edicts is that no line should be wider than 80 characters. Since this is a Rails project, we often have strin

5条回答
  •  半阙折子戏
    2020-12-12 10:29

    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.

提交回复
热议问题