有什么办法能让这看起来好一点吗?

conn.exec 'select attr1, attr2, attr3, attr4, attr5, attr6, attr7 ' +
          'from table1, table2, table3, etc, etc, etc, etc, etc, ' +
          'where etc etc etc etc etc etc etc etc etc etc etc etc etc'

比如,有没有办法暗示串联?


当前回答

这个答案中有一些部分帮助我得到了我需要的东西(简单的多行连接,没有额外的空格),但由于没有实际的答案,我在这里编译它们:

str = 'this is a multi-line string'\
  ' using implicit concatenation'\
  ' to prevent spare \n\'s'

=> "this is a multi-line string using implicit concatenation to eliminate spare
\\n's"

作为奖励,这里有一个使用有趣的HEREDOC语法的版本(通过这个链接):

p <<END_SQL.gsub(/\s+/, " ").strip
SELECT * FROM     users
         ORDER BY users.id DESC
END_SQL
# >> "SELECT * FROM users ORDER BY users.id DESC"

The latter would mostly be for situations that required more flexibility in the processing. I personally don't like it, it puts the processing in a weird place w.r.t. the string (i.e., in front of it, but using instance methods that usually come afterward), but it's there. Note that if you are indenting the last END_SQL identifier (which is common, since this is probably inside a function or module), you will need to use the hyphenated syntax (that is, p <<-END_SQL instead of p <<END_SQL). Otherwise, the indenting whitespace causes the identifier to be interpreted as a continuation of the string.

这并没有节省太多的输入,但对我来说,它看起来比使用+号更好。

此外(我在几年后的编辑中说过),如果您使用Ruby 2.3+,操作符<<~也可用,它从最终字符串中删除额外的缩进。在这种情况下,您应该能够删除.gsub调用(尽管这可能取决于开始缩进和最终需求)。

编辑:再加一个:

p %{
SELECT * FROM     users
         ORDER BY users.id DESC
}.gsub(/\s+/, " ").strip
# >> "SELECT * FROM users ORDER BY users.id DESC"

其他回答

conn.exec [
  "select attr1, attr2, attr3, ...",
  "from table1, table2, table3, ...",
  "where ..."
].join(' ')

这个建议的优点在于,自动缩进器可以适当地缩进文档和长字符串的每个部分。但这是以效率为代价的。

多行字符串有多种语法,你已经读过了。我最喜欢的是perl风格:

conn.exec %q{select attr1, attr2, attr3, attr4, attr5, attr6, attr7
      from table1, table2, table3, etc, etc, etc, etc, etc,
      where etc etc etc etc etc etc etc etc etc etc etc etc etc}

多行字符串以%q开头,后面跟着一个{、[或(,然后以相应的反转字符结束。%q不允许插值;%Q是这样的,所以你可以这样写:

conn.exec %Q{select attr1, attr2, attr3, attr4, attr5, attr6, attr7
      from #{table_names},
      where etc etc etc etc etc etc etc etc etc etc etc etc etc}

我不知道这些多行字符串是怎么叫的我们就叫它们Perl multilines吧。

但是请注意,无论您使用的是Perl多行还是像Mark和Peter建议的那样使用heredocs,最终都会出现潜在的不必要的空格。在我的例子和他们的例子中,“from”和“where”行都包含前导空白,因为它们在代码中的缩进。如果不需要这个空格,那么您必须像现在这样使用连接的字符串。

这个答案中有一些部分帮助我得到了我需要的东西(简单的多行连接,没有额外的空格),但由于没有实际的答案,我在这里编译它们:

str = 'this is a multi-line string'\
  ' using implicit concatenation'\
  ' to prevent spare \n\'s'

=> "this is a multi-line string using implicit concatenation to eliminate spare
\\n's"

作为奖励,这里有一个使用有趣的HEREDOC语法的版本(通过这个链接):

p <<END_SQL.gsub(/\s+/, " ").strip
SELECT * FROM     users
         ORDER BY users.id DESC
END_SQL
# >> "SELECT * FROM users ORDER BY users.id DESC"

The latter would mostly be for situations that required more flexibility in the processing. I personally don't like it, it puts the processing in a weird place w.r.t. the string (i.e., in front of it, but using instance methods that usually come afterward), but it's there. Note that if you are indenting the last END_SQL identifier (which is common, since this is probably inside a function or module), you will need to use the hyphenated syntax (that is, p <<-END_SQL instead of p <<END_SQL). Otherwise, the indenting whitespace causes the identifier to be interpreted as a continuation of the string.

这并没有节省太多的输入,但对我来说,它看起来比使用+号更好。

此外(我在几年后的编辑中说过),如果您使用Ruby 2.3+,操作符<<~也可用,它从最终字符串中删除额外的缩进。在这种情况下,您应该能够删除.gsub调用(尽管这可能取决于开始缩进和最终需求)。

编辑:再加一个:

p %{
SELECT * FROM     users
         ORDER BY users.id DESC
}.gsub(/\s+/, " ").strip
# >> "SELECT * FROM users ORDER BY users.id DESC"

今天的优雅回答:

<<~TEXT
Hi #{user.name}, 

Thanks for raising the flag, we're always happy to help you.
Your issue will be resolved within 2 hours.
Please be patient!

Thanks again,
Team #{user.organization.name}
TEXT

<<-TEXT和<<~TEXT有区别,前者保留了块内的空格,后者则没有。

还有其他的选择。 比如串联等等,但这个更有意义。

如果我错了,请告诉我…

为了避免每一行的圆括号关闭,你可以简单地使用双引号和反斜杠来转义换行:

"select attr1, attr2, attr3, attr4, attr5, attr6, attr7 \
from table1, table2, table3, etc, etc, etc, etc, etc, \
where etc etc etc etc etc etc etc etc etc etc etc etc etc"