Ruby元编程-不使用';t尝试评估内部#{}
有没有什么方法可以使多行字符串不尝试计算内部#{} ? 我希望:Ruby元编程-不使用';t尝试评估内部#{},ruby,metaprogramming,multiline,Ruby,Metaprogramming,Multiline,有没有什么方法可以使多行字符串不尝试计算内部#{} ? 我希望: doc <<-DOC describe "should get sysdate on test: #{test_name}" do it "should get SYSDATE" do conx_bd.sysdate.should_not == NULL end end DOC 我得到了这个错误: NameError: undef
doc <<-DOC
describe "should get sysdate on test: #{test_name}" do
it "should get SYSDATE" do
conx_bd.sysdate.should_not == NULL
end
end
DOC
我得到了这个错误:
NameError: undefined local variable or method `test_name' for main:Object
如果一个字符串用单引号括住了herdoc标识符,那么如果该字符串有一个require,我就会得到这个错误
doc <<'-DOC'
require "spec_helper.rb" # conexión oracle
describe "should get sysdate on test: #{test_name}" do
it "should get SYSDATE" do
conx_bd.sysdate.should_not == NULL
end
end
tks
编辑:
谢谢你的支持,我从答案中得到了两种可能的解决方案
首先,我在定义多行字符串时出错
doc <<-DOC should be <<-doc
doc尝试在#之前使用转义字符。转义字符是\
用单引号将heredoc标识符括起来
doc <<-'DOC'
describe "should get sysdate on test: #{test_name}" do
it "should get SYSDATE" do
conx_bd.sysdate.should_not == NULL
end
end
DOC
doc抱歉,但是在这种情况下获取名称错误意味着字符串确实尝试计算其内部#{},并且没有找到名为test#u name的局部变量或方法。@d1egaaz我没有仔细阅读您的注释。请参阅我对答案的编辑。您可以从您的问题中删除添加的部分。doc我得到此错误名称错误:未定义“doc”的方法“-@”:String,通过一个简单的测试:doc=''doc我在定义字符串时出错,我只需要执行以下操作:
doc <<-DOC should be <<-doc
doc <<-'DOC'
describe "should get sysdate on test: #{test_name}" do
it "should get SYSDATE" do
conx_bd.sysdate.should_not == NULL
end
end
DOC