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dWdnZXN0aW9uLgoK
CgotLSAKScOxYWtpIEJheiBDYXN0aWxsbwo8aWJjQGFsaWF4Lm5ldD4K
 
S

Stefan Rusterholz

Iñaki Baz Castillo said:
And if I use a Hash I'll do the same:

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

Use a hash and lowercase the keys. If you need to preserve the original
casing, do that in the value, e.g. like this: {key.lowercase => [key,
value], ...}
Another approach would be a special Key class which is like String but
has #hash and #eql? redefined to work as if the String was lowercased.
There probably are more approaches.

Regards
Stefan
 
J

Jesús Gabriel y Galán

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 =3D qweqweqwe
Method =3D INVITE
NAT=3Dyes

is the same as:

Tag =3D qweqweqwe
METHOD =3D INVITE
Nat=3Dyes

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

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

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

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

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

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

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

Jesus.
 
I

Iñaki Baz Castillo

El Martes, 15 de Abril de 2008, Jes=FAs Gabriel y Gal=E1n escribi=F3:
Another approach is to store it in a hash, but downcase the key before
storing and retreiving:

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

Thanks, but the problem is exactly the opposite: I receive a request that c=
an=20
contain parameter name in low or uppercase, and I need to process them but=
=20
keeping them untouched to forward the request to other location.

Thanks a lot.

=2D-=20
I=F1aki Baz Castillo
 
I

Iñaki Baz Castillo

El Martes, 15 de Abril de 2008, Stefan Rusterholz escribi=C3=B3:
I=C3=B1aki Baz Castillo said:
And if I use a Hash I'll do the same:

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

Use a hash and lowercase the keys. If you need to preserve the original
casing, do that in the value, e.g. like this: {key.lowercase =3D> [key,
value], ...}

Ah ok, it's a good idea :)

Another approach would be a special Key class which is like String but
has #hash and #eql? redefined to work as if the String was lowercased.

Thanks but I don't understnad this point. Do you mean extending the Hash=20
class? I don't think so since there is not any Key class. Could you please=
=20
explain in more detail you suggestion?

Thanks a lot for all.


=2D-=20
I=C3=B1aki Baz Castillo
 
J

Jesús Gabriel y Galán

El Martes, 15 de Abril de 2008, Jes=FAs Gabriel y Gal=E1n escribi=F3:
Another approach is to store it in a hash, but downcase the key before
storing and retreiving:

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

Thanks, but the problem is exactly the opposite: I receive a request tha= t can
contain parameter name in low or uppercase, and I need to process them b= ut
keeping them untouched to forward the request to other location.

If I understand you correctly, in my snippet above, the variable
method will contain what you receive. You downcase it for searching in
the hash, but still have the original string to pass it along. Am I
right? Maybe I am getting the order of your stuff wrong. Can you post
an example of what you have?

Jesus.
 
J

Jesús Gabriel y Galán

El Martes, 15 de Abril de 2008, Stefan Rusterholz escribi=F3:
I=F1aki Baz Castillo said:
And if I use a Hash I'll do the same:

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

Use a hash and lowercase the keys. If you need to preserve the origina= l
casing, do that in the value, e.g. like this: {key.lowercase =3D> [key= ,
value], ...}

Ah ok, it's a good idea :)

Hey, now I got your requirements !! Please ignore my other post...
Thanks but I don't understnad this point. Do you mean extending the Hash
class? I don't think so since there is not any Key class. Could you plea= se
explain in more detail you suggestion?

What he means is that instead of lowercasing the key before inserting
in the hash, you create a Key class in a way that the hash will think
two keys are the same if they have the same string ignoring case.
Something like (not tested):

class Key
attr_reader :key
def initialize key
@key =3D key
end

def hash
@key.downcase.hash
end

def eql? other
@key.downcase.eql? other.downcase
end
end

and then you populate the hash as:

h[Key.new(method)] =3D [method, values_for_method]

this way you locate the entry in the hash ignoring case and have the
original value in the value of the hash.

Hope I've explained myself...

Jesus.
 
I

Iñaki Baz Castillo

El Martes, 15 de Abril de 2008, Jes=FAs Gabriel y Gal=E1n escribi=F3:
Thanks but I don't understnad this point. Do you mean extending the Ha= sh
class? I don't think so since there is not any Key class. Could you
please explain in more detail you suggestion?

What he means is that instead of lowercasing the key before inserting
in the hash, you create a Key class in a way that the hash will think
two keys are the same if they have the same string ignoring case.
Something like (not tested):

class Key
attr_reader :key
def initialize key
@key =3D key
end

def hash
@key.downcase.hash
end

def eql? other
@key.downcase.eql? other.downcase
end
end

and then you populate the hash as:

h[Key.new(method)] =3D [method, values_for_method]

this way you locate the entry in the hash ignoring case and have the
original value in the value of the hash.

Hope I've explained myself...

Sure, it's cool :)

Thanks a lot to both.


=2D-=20
I=F1aki Baz Castillo
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top