From: Stefano Crocco Date: 2010-07-13T17:56:29+09:00 Subject: Re: I'm trying to get the keys, but why values appear? On Tuesday 13 July 2010, Abder-rahman Ali wrote: > |I'm trying to write a script that lists ONLY the keys in a hash as > |follows: http://pastie.org/private/1ruahb5w05ihsloqwmqeng > | > |The case is that when I run the script, I get the following: > | > |NameAbder-Rahman Ali > |Age26 > | > |While, I'm intending to get > | > |Name > |Age > | > |What am I missing in the script? > | > |Thanks. As you can see from the documentation (for example, with ri Hash#each), Hash#each yields two arguments to the block: the key and the corresponding value. Since your block only takes one argument, ruby puts both in an array, whose to_s method produces a string with the two elements one after the other. If you only want the key, you can use each_key rather than each, since this will only pass the block the keys: personal_information.each_key{|k| puts k} If you want to use each (but you don't need to in your example), you can have the block take two parameters and only use the first (the key): personal_information.each{|key, val| puts key} or have the block take a single argument (which will be an array) and use only the first entry of the array personal_information.each{|i| puts i[0]} I hope this helps Stefano