2011-03-26 82 views
4

我在全局函數中使用heredoc中的beestings時遇到了麻煩。運行時會引發錯誤「Exception: arg2 is not defined」。這裏有一個例子:全局函數中的heredoc中的beestings

ruleset a163x59 { 
    meta { 
    name "Beesting in heredoc" 
    description << 
     Demonstrate the error with beestings in global function heredocs 
    >> 
    author "Steve Nay" 
    logging on 
    } 

    dispatch { 
    } 

    global { 
    myFunc = function(arg1, arg2) { 
     firstString = "This is a regular string: #{arg1}. No problem there."; 
     secondString = << 
      This is a heredoc with a beesting: #{arg2}. Problem! 
     >>; 
     secondString; 
    }; 
    } 

    rule first_rule { 
    select when pageview ".*" setting() 
    pre { 
     msg = myFunc("First argument", "Second argument"); 
    } 
    notify("Testing...", msg) with sticky = true; 
    } 
} 

它從來沒有抱怨arg1被不確定的,這表明,使用普通的字符串內使用beesting就好了。

有沒有我做錯了,或者這是一個錯誤?

+1

獎勵積分(無論如何都是虛擬點...)用於發佈代碼示例。它使得回答問題變得更容易! – TelegramSam 2011-03-26 04:03:05

回答

3

這實際上是一個錯誤,但有一個解決方法。使用此修改後的代碼替換您的函數def:

myFunc = function(arg1, arg2) { 
    firstString = "This is a regular string: #{arg1}. No problem there."; 
    secondString = << 
     This is a heredoc with a beesting: #{arg2}. Problem! 
    >>; 
    "#{secondString}"; 
}; 

請注意,最後一條語句(返回值)是帶引號的beesting。這迫使解決heredoc中的任何問題,並且工作。

發生此問題的原因是KRL延遲綁定了beesting替換,直到javascript執行,但在閉包生成中存在導致變量不可用的錯誤。用引用的beesting強制解決這個問題。

2

我已經在我自己的測試中證實你確實偶然發現了一個錯誤。我會把它歸檔,我們會盡快解決這個問題。謝謝。

+0

太棒了。謝謝你的照顧! – 2011-03-26 02:42:50