Creating InsensitiveHash in C

  • Thread starter Iñaki Baz Castillo
  • Start date
I

Iñaki Baz Castillo

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/Hash.html
but I can't find it in Ruby libs directory.


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

Rick DeNatale

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/Hash.html
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.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
 
I

Iñaki Baz Castillo

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
 

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

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top