-
Notifications
You must be signed in to change notification settings - Fork 4
KeyHash
trans edited this page Nov 11, 2011
·
2 revisions
KeyHash is just like Hash, except that all keys are converted to a uniform type, by default they are Strings.
require 'hashery/key_hash'
h = KeyHash.new
h[:a] = 1
h[:b] = 2
h #=> {'a'=>1, 'b'=>2}
This is true regardless of the type of key used --all will receive the #to_s message upon assignment.
h = KeyHash.new
h[1] = 1
h[nil] = 2
h #=> {'1'=>1, ''=>2}
If you want the keys to be of some other type, you can alter them via the #convert_keys method.
h = KeyHash.new
h.convert_keys{ |k| k.to_sym }
h['a'] => 1
h['b'] => 2
h #=> {:a=>1, :b=>2}
KeyHash used to be called Stash. It was renamed to be more explicit and thus fit better with the rest of Hashery.
Stash used to be a standalone library. In fact, you can still install stash as a separate gem. However you will get a aging discontinued version of the class. All new development is only available via hashery.