From: "Derrick B." Date: 2012-12-29T09:38:25+09:00 Subject: Re: Ruby Koans regarding Hashes. 7stud -- wrote in post #1090511: >> This is one of those "I get it, but then I do not get it" paradoxes. > > You said you know C++ and perl. They both employ the concept of > "references". > It wasn't the reference part that was confusing, just the contents of the block and what it was doing. { |hash, key| hash[key] = [] } >> I understand whatever is returned from the block is assigned to "hash", >> right? > > No. Inside the block, the code assigns an array reference to the key. > Then the block returns a reference to the same array. Two references > to the same array. I had that mostly correct in my head, but two references to the same array??? After creating the new Hash object, the hash variable is just the Hash reference, but the indexed Hash reference ( like hash[:one] ) points to the array reference, right? >> hash = Hash.new { |hash, key| hash[key] = [] } => {} >> hash => {} >> hash[:one] => [] >> hash.object_id => 269913890 >> hash[:one].object_id => 269881860 Two different id's. Or did I do that wrong? Two references to the same array in that assignment expression above doesn't sound right. Your other example: ref1 = [] ref2 = ref1 ref1 << 10 ref2 << 20 ...makes sense of two references to the same array. > > In ruby, the following is an array constructor: > > [] > That I do understand, because Perl is similar: my $a = []; Ok, maybe you are about to tell me otherwise here, too, but it sure looks similar to me! heh perl -le '$a = []; push @$a, "hello"; print @$a' --output-- hello -- Posted via http://www.ruby-forum.com/.