Apache::MultiVal
Apache::MultiVal is a multi-valued datatype for Apache request
parameters. Instances of it are used to represent request parameters in a
parameter table in an Apache mod_ruby application. Apache::MultiVal makes each
parameter in the table capable of being treated like both a String and an Array
by delegating String and Array instance methods to either the first value or the
Array of values, respectively. The instance methods which String and Array share
in common are delegated to the String, except #each, #[], and #[]=, which are
sent to the Array. This allows the code to be kept simple: if you only ever
expect a parameter to have a single value, you can treat it as if it is a
String:
foo = request.paramtable['foo'].downcase
and treat parameters which can have multiple values (mostly) as an Array:
bars = request.paramtable['bar'].collect {|val| val.downcase}
For the methods that Array and String share in common, you can cast the parameter to the object you wish with the normal #to_a and #to_s methods:
foo = request.paramtable['foo']
if foo.to_a.length > 1
request.log_warn( "Request had more than one 'foo' parameter: %s",
foo.to_a.inspect )
Of course, the Array's length could be obtained with foo.nitems, too, since Array#nitems isn't obscured by String's instance methods.
Obscured Array methods
As indicated above, some of Array's methods are obscured by those of String, so you should take special note when using them to be sure you know what you'll be getting. For the version of Ruby that was most recent as of this writing (ruby 1.8.0 (2003-06-27)), these are:
"*", "+", "<<", "<=>", "==", "concat", "delete", "empty?", "eql?", "hash", "include?", "index", "insert", "inspect", "length", "replace", "reverse", "reverse!", "rindex", "size", "slice", "slice!", "to_s"
Keyword(s):
References:[ClassReferenceManual] [Apache::Request]