# Hash, key, problem

**URL:** <https://www.ruby-forum.com/t/hash-key-problem/140766>\
**Category:** Ruby\
**Created:** [June 18, 2008, 11:41am UTC](https://www.ruby-forum.com/t/hash-key-problem/140766 "2008-06-18T11:41:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![fw59](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/7ea924/32.png) [@fw59](https://www.ruby-forum.com/u/fw59)\
**Post date:** [June 18, 2008, 11:41am UTC](https://www.ruby-forum.com/t/hash-key-problem/140766/1 "2008-06-18T11:41:37Z")

</div>

Hallo people,  
I cant get something done since a while. Its very basic stuff… ☹

I have the following hash for example :  
hash = { $-a =\> “info”, $F =\> “another info”}  
and so on, you get the meaning…  
Now i use Hash#each\_pair so i get the value of the variables and the  
info, but I would like also to get a simple string, containing  
the name of the variable, so it should look like this :  
$-a  
info  
false

$F  
another info  
nil

But I cant get it done. Its probably very stupid question, but doesnt  
matter what I try - i dont get the name of the key, i always get the  
key itself returned… Can somebody help me?  
Thanks in advance.

Katja

---

<div class="post-metadata">

**Author:** ![fw59](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/7ea924/32.png) [@fw59](https://www.ruby-forum.com/u/fw59)\
**Post date:** [June 18, 2008, 11:51am UTC](https://www.ruby-forum.com/t/hash-key-problem/140766/2 "2008-06-18T11:51:02Z")

</div>

On 18 Jun., 11:39, psy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> info  
> false
> 
> $F  
> another info  
> nil
> 
> But I cant get it done. Its probably very stupid question, but doesnt  
> matter what I try - i dont get the name of the key, i always get the  
> key itself returned… Can somebody help me?  
> Thanks in advance.

Then just put the names in the Hash as keys.

hash = { “$-a” =\> “info”, “$F” =\> “another info”}  
hash = { “$-a” =\> [$-a, “info”], “$F” =\> [$F, “another info”]}

Btw, what are you trying to accomplish?

robert

---

<div class="post-metadata">

**Author:** ![fw59](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/7ea924/32.png) [@fw59](https://www.ruby-forum.com/u/fw59)\
**Post date:** [June 18, 2008, 11:51am UTC](https://www.ruby-forum.com/t/hash-key-problem/140766/3 "2008-06-18T11:51:38Z")

</div>

On Wed, Jun 18, 2008 at 3:09 PM, psy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> info  
> false
> 
> $F  
> another info  
> nil

Do you want something like this:

hash={:name =\> “John D.”, :age =\> 23}

hash.each do |elem|  
puts elem.join(“\n”)  
end

That should work. You can also of course get the string as:

string=elem.join(“\n”)

Hope that helps.

Jay

---

<div class="post-metadata">

**Author:** ![fw59](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/7ea924/32.png) [@fw59](https://www.ruby-forum.com/u/fw59)\
**Post date:** [June 18, 2008, 12:05pm UTC](https://www.ruby-forum.com/t/hash-key-problem/140766/4 "2008-06-18T12:05:49Z")

</div>

On Wed, 18 Jun 2008 18:48:50 +0900  
Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Then just put the names in the Hash as keys.
> 
> hash = { “$-a” =\> “info”, “$F” =\> “another info”}  
> hash = { “$-a” =\> [$-a, “info”], “$F” =\> [$F, “another info”]}
> 
> Btw, what are you trying to accomplish?
> 
> robert

_blush_  
Oh, man… Thank you.  
To your question : Im trying to make a small program, which prints  
well formated variable-info in the console. Stupid stuff…

Katja

---

<div class="post-metadata">

**Author:** ![fw59](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/7ea924/32.png) [@fw59](https://www.ruby-forum.com/u/fw59)\
**Post date:** [June 18, 2008, 12:07pm UTC](https://www.ruby-forum.com/t/hash-key-problem/140766/5 "2008-06-18T12:07:35Z")

</div>

On Wed, 18 Jun 2008 18:49:53 +0900  
“Srijayanth S.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> string=elem.join(“\n”)
> 
> Hope that helps.
> 
> Jay

Yeah, this would work, but now there is another problem - it dont prints  
the value of the variables.  
Btw I got it done.

Thank you anyway

Katja
