2

内側の #{} を評価しようとしない複数行の文字列を作成する方法はありますか?

私は次のものが欲しい:

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 
  1. # を \ でエスケープ

    <<-doc
    describe "should get sysdate on test: #{test_name}" do it "should get SYSDATE" do conx_bd.sysdate.should_not == NULL end end
    doc

  2. ヒアドキュメント識別子を一重引用符で囲みます

    <<-'doc'
    describe "should get sysdate on test: #{test_name}" do it "should get SYSDATE" do conx_bd.sysdate.should_not == NULL end end
    doc

4

2 に答える 2

3

ヒアドキュメント識別子を一重引用符で囲みます。

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

編集一重引用符の開始とハイフンの相対的な位置が間違っていました。steenslag は正しかった。修理済み。

于 2012-10-18T21:31:53.917 に答える
2

Try the with the escape character before #. The escape character is \.

于 2012-10-18T21:29:43.940 に答える