内側の #{} を評価しようとしない複数行の文字列を作成する方法はありますか?
私は次のものが欲しい:
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) を作成するには: (メタプログラミング用です)
describe "should get sysdate on test: #{test_name}" do
it "should get SYSDATE" do
conx_bd.sysdate.should_not == NULL
end
end
私はこのエラーが発生しています:
NameError: undefined local variable or method `test_name' for main:Object
ヒアドキュメント識別子を一重引用符で囲む場合、文字列にたとえば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
文書
エラー:
LoadError: no such file to load -- spec_helper
tks
編集: ご支援いただきありがとうございます。回答から 2 つの解決策が得られます。
まず、複数行の文字列の定義にエラーがあります
doc <<-DOC should be <<-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'
describe "should get sysdate on test: #{test_name}" do it "should get SYSDATE" do conx_bd.sysdate.should_not == NULL end end
doc