From: SpringFlowers AutumnMoon Date: 2007-09-18T22:21:56+09:00 Subject: Re: how to use tuple as hash key Rick Denatale wrote: > On 9/18/07, SpringFlowers AutumnMoon wrote: > I think you might be tripping over the difference between a variable > and an object here. I know that lots of posters to this forum do. > Variables aren't objects per se. A variable references an object. > Variable assignment copies the reference NOT the object. actually, my posting of "The meaning of a = b in object oriented languages" reminded me of what I learned in Java... that the variables are not objects themselves. the variable merely references an object, like a pointer to an object. the main confusion I had with hash (and i think now clear) is that Hash first used hash() on the key's value to find a hash slot. and then at the slot, the mapping is done not for the key's value to the final value. the mapping merely stores the pointer to the key object, and the pointer to the value object. so if the key object changes, then the hash() will most likely hash to another hash slot, and then found that there is no mapping there, and therefore store another pointer to key, pointer to value pair there. thus the result: irb(main):084:0> a = [1,2] => [1, 2] irb(main):085:0> h = {} => {} irb(main):086:0> h[a] = "ha" => "ha" irb(main):087:0> a[1] = 99 => 99 irb(main):088:0> a => [1, 99] irb(main):089:0> h[a] = "hee" => "hee" irb(main):090:0> h => {[1, 99]=>"ha", [1, 99]=>"hee"} irb(main):091:0> h[[1,2]] => nil irb(main):092:0> h[[1,99]] => "hee" irb(main):093:0> h.rehash => {[1, 99]=>"hee"} irb(main):094:0> Example 2: irb(main):094:0> h = {} => {} irb(main):095:0> b = "haha" => "haha" irb(main):096:0> h["a"] = b => "haha" irb(main):097:0> h => {"a"=>"haha"} irb(main):098:0> b[3] = "i" => "i" irb(main):099:0> h => {"a"=>"hahi"} irb(main):100:0> b = "another" => "another" irb(main):101:0> h => {"a"=>"hahi"} irb(main):102:0> -- Posted via http://www.ruby-forum.com/.