2010-09-02 56 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]'}来完成。

相关问题