# I'm trying to get the keys, but why values appear?

**URL:** <https://www.ruby-forum.com/t/im-trying-to-get-the-keys-but-why-values-appear/191475>\
**Category:** Ruby\
**Created:** [July 13, 2010, 10:40am UTC](https://www.ruby-forum.com/t/im-trying-to-get-the-keys-but-why-values-appear/191475 "2010-07-13T10:40:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 13, 2010, 10:40am UTC](https://www.ruby-forum.com/t/im-trying-to-get-the-keys-but-why-values-appear/191475/1 "2010-07-13T10:40:36Z")

</div>

I’m trying to write a script that lists ONLY the keys in a hash as  
follows: [http://pastie.org/private/1ruahb5w05ihsloqwmqeng](http://pastie.org/private/1ruahb5w05ihsloqwmqeng)

The case is that when I run the script, I get the following:

NameAbder-Rahman A.  
Age26

While, I’m intending to get

Name  
Age

What am I missing in the script?

Thanks.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 13, 2010, 10:57am UTC](https://www.ruby-forum.com/t/im-trying-to-get-the-keys-but-why-values-appear/191475/2 "2010-07-13T10:57:02Z")

</div>

On Tuesday 13 July 2010, Abder-rahman Ali wrote:

> |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

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 13, 2010, 10:58am UTC](https://www.ruby-forum.com/t/im-trying-to-get-the-keys-but-why-values-appear/191475/3 "2010-07-13T10:58:48Z")

</div>

On Tue, Jul 13, 2010 at 4:40 PM, Abder-rahman Ali  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> What am I missing in the script?

the fun 🙂

> personal\_information.each {|key| p key}  
> [“Name”, “Abder-Rahman A.”]  
> [“Age”, “26”]

> personal\_information.each {|key| p key.first}  
> “Name”  
> “Age”

> personal\_information.each\_key {|key| p key}  
> “Name”  
> “Age”

> personal\_information.each\_value {|key| p key}  
> “Abder-Rahman A.”  
> “26”

> personal\_information.each {|key,value| p key}  
> “Name”  
> “Age”

> personal\_information.each {|key,value| p value}  
> “Abder-Rahman A.”  
> “26”

kind regards -botp

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 13, 2010, 11:21am UTC](https://www.ruby-forum.com/t/im-trying-to-get-the-keys-but-why-values-appear/191475/4 "2010-07-13T11:21:46Z")

</div>

Thanks a lot all for your replies. It really helps.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 13, 2010, 11:02am UTC](https://www.ruby-forum.com/t/im-trying-to-get-the-keys-but-why-values-appear/191475/5 "2010-07-13T11:02:19Z")

</div>

If you want the keys only you can do this  
“personal\_information.keys.each do |key|” or  
“personal\_information.each\_key do |key|”

This, however, “personal\_information.each do |key|” returns each  
element of the hash as an array in the form [key, value]

Look here for the documentation (  
[class Hash - RDoc Documentation](http://www.ruby-doc.org/core/classes/Hash.html) ), in fact this should  
be the first place you look when you have a problem.
