unlike perl, ruby treats 1 and ‘1’ differently in hash keys:
$ irb
irb(main):001:0> a={}
irb(main):002:0> a[1]=1
irb(main):003:0> a[‘1’]='1’
irb(main):004:0> a
{1=>1, “1”=>“1”}
so what are the range the valid values for hash keys? can i, like in
python, use “immutable lists”/tuples as hash keys?
···
–
dave
“David Garamond” davegaramond@icqmail.com wrote in message
news:3D398A76.8090504@icqmail.com…
so what are the range the valid values for hash keys? can i, like in
python, use “immutable lists”/tuples as hash keys?
As Ned Konz has already replied, you can use any object as a hash key since
they all respond to the “hash” message. But since Ruby arrays (and strings,
for that matter) aren’t immutable you want to be careful about using them as
hash keys. Specifically, if you use a mutable object as a hash key and then
change its value, be sure to call rehash on the Hash object. See this page
from “Programming Ruby”:
http://www.rubycentral.com/book/ref_c_hash.html#Hash.rehash
for a short and sweet example.