[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Creating InsensitiveHash in C

Iñaki Baz Castillo

2/19/2009 8:58:00 PM

Hi, I'd like a faster Ruby class same as Hash but in which key and value ar=
e=20
insensitive, so:

=2D---------------------------------
params =3D InsensitiveHash.new
params["q"] =3D 0.5
params["transport"] =3D "TCP"

params["Q"]
=3D> 0.5

params["Transport"] =3D=3D "tcp"
=3D> true
=2D---------------------------------

It will be used extensively so I need it being very fast. I'm thinking in=20
implementing it as a new Ruby class in C, modifying Hash class.

Do you think it's feasible? Any suggestion?
Thanks a lot.

PD: Where is the C file defining Hash class? I read "hash.c" in Hash=20
documentation:
http://www.ruby-doc.org/core/classes...
but I can't find it in Ruby libs directory.


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

2 Answers

Rick DeNatale

2/19/2009 9:23:00 PM

0

On Thu, Feb 19, 2009 at 3:57 PM, I=F1aki Baz Castillo <ibc@aliax.net> wrote=
:

> Hi, I'd like a faster Ruby class same as Hash but in which key and value
> are
> insensitive, so:
>
> ----------------------------------
> params =3D InsensitiveHash.new
> params["q"] =3D 0.5
> params["transport"] =3D "TCP"
>
> params["Q"]
> =3D> 0.5
>
> params["Transport"] =3D=3D "tcp"
> =3D> true
> ----------------------------------
>
> It will be used extensively so I need it being very fast. I'm thinking in
> implementing it as a new Ruby class in C, modifying Hash class.
>
> Do you think it's feasible? Any suggestion?
> Thanks a lot.
>
> PD: Where is the C file defining Hash class? I read "hash.c" in Hash
> documentation:
> http://www.ruby-doc.org/core/classes...
> but I can't find it in Ruby libs directory.
>

You won't find it in a runtime installation of Ruby. The file hash.h is in
the top level directory of the Ruby source code.

That said, If all you want to do is to make the keys case insensitive, a
naive Ruby implementation is likely to be almost as fast as anything you
could do in C, since the time intensitve implementation parts of Hash are
already in C.

So something like

class InsensitiveHash
def initialize
@hash =3D {}
end

def [](key}
@hash[key.downcase]
end

def []=3D(key, value)
@hash[key.downcase] =3D value
end
end

And yes, I'd recommend having the InsensitiveHash have an instance of Hash
rather than being a subclass of Hash.

--=20
Rick DeNatale

Blog: http://talklikeaduck.denh...
Twitter: http://twitter.com/Ri...
WWR: http://www.workingwithrails.com/person/9021-ric...
LinkedIn: http://www.linkedin.com/in/ri...

Iñaki Baz Castillo

2/19/2009 9:31:00 PM

0

El Jueves, 19 de Febrero de 2009, Rick DeNatale escribi=C3=B3:
> class InsensitiveHash
> =C2=A0 =C2=A0 def initialize
> =C2=A0 =C2=A0 =C2=A0 =C2=A0@hash =3D {}
> =C2=A0 =C2=A0 end
>
> =C2=A0 =C2=A0def [](key}
> =C2=A0 =C2=A0 =C2=A0 @hash[key.downcase]
> =C2=A0 =C2=A0end
>
> =C2=A0 =C2=A0def []=3D(key, value)
> =C2=A0 =C2=A0 =C2=A0 @hash[key.downcase] =3D value
> =C2=A0 =C2=A0end
> end
>
> And yes, I'd recommend having the InsensitiveHash have an instance of Hash
> rather than being a subclass of Hash.

The fact is I already have something as above, but I wonder if using a new=
=20
Ruby C native class would be *really* faster.

Thanks a lot.

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