Hello –
cgi = CGI.new
year = cgi[‘year’] => [‘2002’]
In the CGI module, all the arguments are given as arrays instead of
scalars (presumably to support the case where the HTML page had
multiple tags of the same name).
How can I get it as a scalar, though? Right now I’m using:
year ,= cgi[‘year’]
Interesting – I don’t think I’ve ever seen it written with the comma
up against the =
(as opposed to: year, = cgi[‘year’])
You can also do:
year = cgi[‘year’][0]
By the way, there was a long thread in May about this, specifically
about the relative merits of different (from the current, and from
each other) syntax possibilities for this. (Search for “cgi params
api” on http://www.ruby-talk.org.)
Is this a safe syntax to use (i.e. will it be valid in future versions
of Ruby)? It seems to be an abuse of the comma notation (I would think
that the comma is meant to separate lvalues).
It makes sense in the context of some more examples:
a,b,c = 1,2,3 # a == 1, b == 2, c == 3
a,b = 1,2,3 # a == 1, b == 2, 3 discarded
a, = 1,2,3 # a == 1, 2 and 3 discarded
a = 1,2,3 # a == [1,2,3]
a,*b = 1,2,3 # a == 1, b == [2,3]
So the “a, =” notation (third example) is a sort of reduced or
boundary case of “a,b,c,… =”
David
···
On Tue, 2 Jul 2002, Philip Mak wrote:
–
David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav