Re: Hash or bidimensional Array when key is case insensitive?



On Tue, Apr 15, 2008 at 6:31 PM, Iñaki Baz Castillo <ibc@xxxxxxxxx> wrote:
Hi, I've to take a decission about using an bidimensional Array or a
Hash to store parameters. Of course I prefer Hash since the order
doesn't matter and I know the param name I'll need.
The problem is that the param name is case insensitive.

Example:

tag = qweqweqwe
Method = INVITE
NAT=yes

is the same as:

Tag = qweqweqwe
METHOD = INVITE
Nat=yes

If I use an bidimensional Array and need to get "method" param I willl
need to do:

method = params_array.find { |param| param[0] =~ /^method$/i }[1]

And if I use a Hash I'll do the same:

method = params_hash.find { |param| param[0] =~ /^method$/i }[1]

Is there any advantage using Array or Hash in my case (key is case insensitive)?

Another approach is to store it in a hash, but downcase the key before
storing and retreiving:

irb(main):001:0> h = {"get" => "value_for_get", "post" => "value_for_post"}
=> {"get"=>"value_for_get", "post"=>"value_for_post"}
irb(main):002:0> method = "gEt"
=> "gEt"
irb(main):003:0> h[method.downcase]
=> "value_for_get"

Jesus.

.



Relevant Pages

  • [Patch] [LTC] ECC <= 112
    ... Please note that these short hash codes are NOT secure. ... To keep signature size small, ... @param out The destination for the signature ... goto LBL_ERR; ...
    (sci.crypt)
  • Re: How to create "def method(item)= (value)" ?
    ... do as I wrote further on and have the param function return ... def initialize hash ... validation code, or having the object.param=value update something ...
    (comp.lang.ruby)
  • Re: How to create "def method(item)= (value)" ?
    ... do as I wrote further on and have the param function return ... def initialize hash ... validation code, or having the object.param=value update something ...
    (comp.lang.ruby)
  • Hash or bidimensional Array when key is case insensitive?
    ... Hash to store parameters. ... The problem is that the param name is case insensitive. ... Tag = qweqweqwe ... If I use an bidimensional Array and need to get "method" param I willl ...
    (comp.lang.ruby)