2010-09-02 68 views
2

我試圖在我的黃瓜測試中嘲笑OpenID處理。爲了這個目的,我用下面的方法:如何將散列傳遞給Ruby 1.8.7的class_eval'ed方法?

def set_result_of_openid_authentication(result_type, profile_data = nil) 
    ActionController::Base.class_eval " 
    def begin_open_id_authentication(identity_url, options = {}) 
     yield [OpenIdAuthentication::Result.new('#{result_type}'.to_sym), identity_url, #{profile_data}] 
    end 
    " 
end 

# example usage 
set_result_of_openid_authentication :successful, 'email' => '[email protected]' 

這正常使用Ruby 1.9.2,但使用Ruby 1.8.7,我得到以下編譯錯誤:

(eval):5:in `set_result_of_openid_authentication': compile error 
(eval):3: syntax error, unexpected tIVAR, expecting kDO or '{' or '('  
...identity_url, [email protected]] 

出於某種原因,哈希沒有保存......是否有一些解決方法可以使它與紅寶石一起工作?

謝謝。

回答

1

它看起來像問題是,你class_eval插值的字符串中#{profile_data}即將通過爲[email protected]這是一個1.8.7的Hashto_s表示。

如果您將其替換爲#{profile_data.inspect},則應根據需要按{'email' => '[email protected]'}來完成。