Incompatible changes to cgi.rb in 1.8.0

Discussion in 'Ruby' started by Brian Candler, Aug 4, 2003.

  1. Hmm, something has changed in CGI which has broken an application - sorry I
    didn't have a chance to test/comment on this before 1.8.0 was released, but
    anyway here's the information now.

    @cgi['foo'][0] used to return nil if parameter 'foo' was not supplied by the
    user. It now returns empty string; so does @cgi['foo']

    This is a bit of a problem, since I have code which says

    if @cgi['foo'][0]
    .. do stuff if parameter 'foo' is supplied
    end

    in lots of places. But in any case, surely "parameter is missing" should be
    represented by nil, since the condition "parameter is present but is an
    empty string" is different?

    The solution I have found is

    if @cgi.params['foo'][0]
    ..
    endif

    But that seems to be rather against the spirit of the change to the cgi.rb
    module, which was supposed to make accessing parameters *easier*, not more
    convoluted...

    Cheers,

    Brian.
     
    Brian Candler, Aug 4, 2003
    #1
    1. Advertisements

  2. Brian Candler

    Dan Doel Guest

    What about:

    if @cgi.has_key? 'foo'
    ... do stuff
    end
     
    Dan Doel, Aug 4, 2003
    #2
    1. Advertisements

  3. I found another set of places where this change breaks things:

    foo( @cgi['bar'][0] || 'default value' )

    no longer works, because the left-hand side returns empty string. I am very
    happy with the practice of "nil is false, strings (including empty) and
    numbers (including zero) are true" but it doesn't work here any more :-(

    Still, a global search-replace of
    @cgi[
    with
    @cgi.params[
    seems to have done the trick.

    Regards,

    Brian.
     
    Brian Candler, Aug 5, 2003
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.