有没有办法让这个看起来更好一些?
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'
比如说,有没有一种方式可以暗示字符串拼接?
有没有办法让这个看起来更好一些?
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'
比如说,有没有一种方式可以暗示字符串拼接?
"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"
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'
<<是字符串连接运算符
+
是常规的连接运算符,<<
是 原地 追加运算符。在一个字面量上使用副作用确实可以起作用(第一个字符串被修改了两次并返回了),但是我认为这很奇怪,让我产生了疑惑,“+”则非常清晰明了。但也许这只是因为我对 Ruby 还不是很熟悉... - Beni Cherniavsky-Paskinfrozen_string_literal
,这将无法工作。 - Raidoconn.exec %w{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} * ' '
(在插值字符串中使用%W)
conn.exec [
"select attr1, attr2, attr3, ...",
"from table1, table2, table3, ...",
"where ..."
].join(' ')
<<~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”之间有区别,前者保留块内的空格,后者不保留。
还有其他选项,比如连接等,但总体而言这个更有意义。
如果我在这里说错了,请告诉我该怎么做...
和你一样,我也在寻找一种不包含换行符的解决方案。(虽然它们在 SQL 中可能是安全的,但对于我的情况来说不安全,因为我需要处理大块的文本)
这种方法可能同样丑陋,但你可以在 heredoc 中反斜杠转义换行符,以将它们从生成的字符串中省略:
conn.exec <<~END_OF_INPUT
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
END_OF_INPUT
<<~'END_OF_INPUT'
)执行此操作,请小心。 在这里将评估#{expressions}
,而它们在您的原始代码中不会被评估。出于这个原因,A. Wilson的回答可能更好。# to print multi line string
print("""
hello i am mario and this
is multi line it is same in python
""")