[英]How can I pass a hash into a class_eval'ed method with Ruby 1.8.7?
我正在尝试在黄瓜测试中模拟OpenID处理。 为此,我使用以下方法:
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' => 'dhofstet@example.com'
这在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, emaildhofstet@example.com]
由于某种原因,哈希值未保留。是否存在一些变通办法,使其可以同时与两个红宝石一起使用?
谢谢。
看来问题是在您的class_eval
内部,内插字符串#{profile_data}
是作为emaildhofstet@example.com
通过的,它是1.8.7中Hash
的to_s
表示。
如果将其替换为#{profile_data.inspect}
则它应根据需要以{'email' => 'dhofstet@example.com'}
形式出现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.