Evan Martin (evan) wrote in evan_tech,
Evan Martin

weird type flexibility (ruby-openid and chr)

So a couple of others have had trouble with the ruby-openid library. The problem is that starting up the whole thing with:
fails with
undefined method `chr' for "foo":String
/var/lib/gems/1.8/gems/ruby-openid-2.0.2/lib/openid/yadis/xri.rb:16:in `identifier_scheme'
/var/lib/gems/1.8/gems/ruby-openid-2.0.2/lib/openid/consumer/discovery.rb:474:in `discover'
/var/lib/gems/1.8/gems/ruby-openid-2.0.2/lib/openid/consumer.rb:333:in `discover'
/usr/lib/ruby/1.8/rubygems/custom_require.rb:27:in `to_proc'
/var/lib/gems/1.8/gems/ruby-openid-2.0.2/lib/openid/consumer/discovery_manager.rb:51:in `get_next_service'
/var/lib/gems/1.8/gems/ruby-openid-2.0.2/lib/openid/consumer.rb:222:in `begin'

The problem is due to this interesting behavior of the CGI module.
Say you have a CGI parameter called 'foo' with the value of 'bar'. Then the following things are true:
p cgi['foo']            # => 'bar'
p cgi['foo'].class      # => String
p cgi['foo'][0]         # => 'bar'
p cgi['foo'].to_s[0]    # => 'bar'
p cgi['foo'].to_str     # => 'bar'
p cgi['foo'].to_str[0]  # => 'bar'
This is due to a questionable hack in the CGI module, where the string object is extended with a new method that breaks the string interface. It turns out it prints a warning if you try this:
/var/lib/gems/1.8/gems/ruby-openid-2.0.2/lib/openid/yadis/xri.rb:16:in `identifier_scheme':CAUTION! cgi['key'] == cgi.params['key'][0]; if want Array, use cgi.params['key']
But you're typically using OpenID in a web server context and that warning apparently isn't enough to trigger the "internal server error" result.

The fix is to instead use
where #params just gives you the parameter value in a hash (with a default value of [] so it's safe to index without checking for the key).

I'm clearly rusty on this whole "dynamic language" thing, because I found the above kinda confusing. (Haskell and C++, which are what I mostly hack these days, are pretty different but also quite similar in some aspects of what you can't do.) Hopefully I've stuck enough text in this post for future Googler-seekers to see the problem.
Tags: ruby

  • your vcs sucks

    I've been hacking on some Haskell stuff lately that's all managed in darcs and it's reminded me of an observation I made over two years ago now (see…

  • jaunty upgrade

    Upgraded to Ubuntu Jaunty. X hung with garbage on the screen after rebooting. Booted into "restore mode" (I forget the exact name), it gave me a…

  • ubuntu summit

    There's an Ubuntu summit going on in Mountain View next week. I'll be there Monday and plan to meet up with someone who's interested in packaging…

  • Post a new comment


    default userpic
    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.