开发者

How can I pass a hash into a class_eval'ed method with Ruby 1.8.7?

I'm trying to mock the OpenID handling in my cucumber tests. For this purpose I use the following method:

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'

This works fine with Ruby 1.9.2, but with Ruby 1.8.7 I get the following compile error:

(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]

For some reason the hash is not preserved... Is there some workaround to make it work开发者_运维技巧 with both Rubies?

Thanks.


It looks like the problem is that inside your class_eval the interpolated string #{profile_data} is coming through as emaildhofstet@example.com which is the to_s representation of a Hash in 1.8.7.

If you replace this with #{profile_data.inspect} then it should come come through as {'email' => 'dhofstet@example.com'} as required.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜