我正在尝试制作一个Ruby heredoc的问题.它正在返回每行的前导空格,即使我包含 - 运算符,它应该抑制所有前导空白字符.我的方法看起来像这样:
def distinct_count
<<-EOF
\tSELECT
\t CAST('#{name}' AS VARCHAR(30)) as COLUMN_NAME
\t,COUNT(DISTINCT #{name}) AS DISTINCT_COUNT
\tFROM #{table.call}
EOF
end
Run Code Online (Sandbox Code Playgroud)
我的输出如下:
=> " \tSELECT\n \t CAST('SRC_ACCT_NUM' AS VARCHAR(30)) as
COLUMN_NAME\n \t,COUNT(DISTINCT SRC_ACCT_NUM) AS DISTINCT_COUNT\n
\tFROM UD461.MGMT_REPORT_HNB\n"
Run Code Online (Sandbox Code Playgroud)
当然,这在这个特定的例子中是正确的,除了第一个"和\ t之间的所有空格.有没有人知道我在这里做错了什么?
说我希望有一个非常大的漂亮打印的html代码段与我的ruby代码内联.什么是最干净的方法来做到这一点,而不会丢失我的字符串中的任何格式或不得不记住某种gsub正则表达式.
将它们编码为一行很容易,但很难阅读:
1.times do
# Note that the spaces have been changed to _ so that they are easy to see here.
doc = "\n<html>\n__<head>\n____<title>\n______Title\n____</title>\n__</head>\n__<body>\n____Body\n__</body>\n</html>\n"
ans = "Your document: %s" % [doc]
puts ans
end
Run Code Online (Sandbox Code Playgroud)
ruby中的多行文本更容易阅读,但字符串不能与其余代码一起缩进:
1.times do
doc = "
<html>
<head>
<title>
Title
</title>
</head>
<body>
Body
</body>
</html>
"
ans = "Your document: %s" % [doc]
puts ans
end
Run Code Online (Sandbox Code Playgroud)
例如,以下是我的代码缩进,但字符串现在每行前面有四个额外的空格:
1.times do
doc = <<-EOM
<html>
<head>
<title>
Title
</title>
</head>
<body>
Body
</body>
</html>
EOM
ans …Run Code Online (Sandbox Code Playgroud)