# Some help needed with understanding ruby hashes

**URL:** <https://www.ruby-forum.com/t/some-help-needed-with-understanding-ruby-hashes/107773>\
**Category:** Ruby\
**Created:** [June 26, 2007, 3:04am UTC](https://www.ruby-forum.com/t/some-help-needed-with-understanding-ruby-hashes/107773 "2007-06-26T03:04:16Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![vfcforums](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/85e7bf/32.png) [@vfcforums](https://www.ruby-forum.com/u/vfcforums)\
**Post date:** [June 26, 2007, 3:04am UTC](https://www.ruby-forum.com/t/some-help-needed-with-understanding-ruby-hashes/107773/1 "2007-06-26T03:04:16Z")

</div>

If a ruby hash rubyhash has a key such as “hashkey” is there a way of  
retrieving the value by the expression rubyhash[“hashkey”] ?

It only seems logical that is rubyhash.has\_key?(“hashkey”) returns true  
rubyhash[“hashkey”] should return a value, but it always gives an error.

I know the same can be achieved via with rubyhash.values\_at(“hashkey”)  
but it appears so inelegant to me.

I am trying to create an erb template which will use the hash keys here  
such as directory1 directory2 for each directory and the for each  
directory use hash key s such as directory, archivename etc, but I find  
values\_at rather inelegant.

I sorely hope there is some aspect to ruby hashes syntax that I am  
missing.

directories :  
directory1 :  
directory : /thisdirectory/yada/yada/yada  
archivename : archivename01  
updatemode : updatemode01  
encryptkey : encryptkey01  
configdir : configdir01  
workdir : workingdir01  
storagedir : storagedir01  
logsdir : logsdir01

```
directory2 :
   directory : /thisdirectory/yada2/yada2/yada2
   archivename : archivename02
   updatemode : updatemode02
   encryptkey : encryptkey02
   configdir : configdir02
   workdir : workingdir02
   storagedir : storagedir02
   logsdir : logsdir02
```

---

<div class="post-metadata">

**Author:** ![vfcforums](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/85e7bf/32.png) [@vfcforums](https://www.ruby-forum.com/u/vfcforums)\
**Post date:** [June 26, 2007, 4:03am UTC](https://www.ruby-forum.com/t/some-help-needed-with-understanding-ruby-hashes/107773/2 "2007-06-26T04:03:09Z")

</div>

On 6/26/07, Frank C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> If a ruby hash rubyhash has a key such as “hashkey” is there a way of  
> retrieving the value by the expression rubyhash[“hashkey”] ?
> 
> It only seems logical that is rubyhash.has\_key?(“hashkey”) returns true  
> rubyhash[“hashkey”] should return a value, but it always gives an error.

This gives you an error?

rubyhash = {“hashkey”=\>“hashvalue”}  
p rubyhash[“hashkey”]

Harry

---

<div class="post-metadata">

**Author:** ![vfcforums](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/85e7bf/32.png) [@vfcforums](https://www.ruby-forum.com/u/vfcforums)\
**Post date:** [June 26, 2007, 4:13am UTC](https://www.ruby-forum.com/t/some-help-needed-with-understanding-ruby-hashes/107773/3 "2007-06-26T04:13:10Z")

</div>

On Jun 25, 2007, at 8:04 PM, Frank C. wrote:

> If a ruby hash rubyhash has a key such as “hashkey” is there a way of  
> retrieving the value by the expression rubyhash[“hashkey”] ?
> 
> It only seems logical that is rubyhash.has\_key?(“hashkey”) returns  
> true  
> rubyhash[“hashkey”] should return a value, but it always gives an  
> error.

What you described is the standard Ruby behavior:

$ irb

> > hash = Hash.new  
> > =\> {}
> 
> > hash[“some\_key”]  
> > =\> nil
> 
> > hash[“some\_key”] = “I’m set now!”  
> > =\> “I’m set now!”
> 
> > hash.has\_key? “some\_key”  
> > =\> true
> 
> > hash[“some\_key”]  
> > =\> “I’m set now!”

Note how I tried that out in the irb shell. It lets you enter Ruby  
expressions and examine their output. This is a powerful ally when  
learning the rules of Ruby.

In short, something else is going on in your code. Hashes work as  
you expect.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![vfcforums](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/85e7bf/32.png) [@vfcforums](https://www.ruby-forum.com/u/vfcforums)\
**Post date:** [June 26, 2007, 11:27am UTC](https://www.ruby-forum.com/t/some-help-needed-with-understanding-ruby-hashes/107773/4 "2007-06-26T11:27:07Z")

</div>

Frank C. wrote:

> If a ruby hash rubyhash has a key such as “hashkey” is there a way of  
> retrieving the value by the expression rubyhash[“hashkey”] ?
> 
> It only seems logical that is rubyhash.has\_key?(“hashkey”) returns true  
> rubyhash[“hashkey”] should return a value, but it always gives an error.
> 
> I know the same can be achieved via with rubyhash.values\_at(“hashkey”)  
> but it appears so inelegant to me.
> 
> I am trying to create an erb template which will use the hash keys here  
> such as directory1 directory2 for each directory and the for each  
> directory use hash key s such as directory, archivename etc, but I find  
> values\_at rather inelegant.
> 
> I sorely hope there is some aspect to ruby hashes syntax that I am  
> missing.

I tracked the error, I was testing the using the concatenating strings  
without using to\_s in the code. ie.

puts "hashkey value is " + rubyhash[“hashkey”]

instead of

"hashkey value is " + (rubyhash[“hashkey”]).to\_s

Regards

Frank

---

<div class="post-metadata">

**Author:** ![vfcforums](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/85e7bf/32.png) [@vfcforums](https://www.ruby-forum.com/u/vfcforums)\
**Post date:** [June 26, 2007, 4:33am UTC](https://www.ruby-forum.com/t/some-help-needed-with-understanding-ruby-hashes/107773/5 "2007-06-26T04:33:11Z")

</div>

On Jun 25, 2007, at 9:04 PM, Frank C. wrote:

> but it appears so inelegant to me.  
> What you say you are doing works for me.

```
rubyhash = { "hashkey" => "value"}
rubyhash["hashkey"] # => "value"

```

I can only conclude that your problem is really other than what you  
think it is. I think you would be well advised to post the actual  
code that is producing the error.

Regards, Morton

---

<div class="post-metadata">

**Author:** ![vfcforums](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/85e7bf/32.png) [@vfcforums](https://www.ruby-forum.com/u/vfcforums)\
**Post date:** [June 26, 2007, 1:45pm UTC](https://www.ruby-forum.com/t/some-help-needed-with-understanding-ruby-hashes/107773/6 "2007-06-26T13:45:20Z")

</div>

Morton G. wrote:

> On Jun 26, 2007, at 5:27 AM, Frank C. wrote:
> 
> > I tracked the error, I was testing the using the concatenating strings  
> > without using to\_s in the code. ie.
> > 
> > puts "hashkey value is " + rubyhash[“hashkey”]
> > 
> > instead of
> > 
> > "hashkey value is " + (rubyhash[“hashkey”]).to\_s
> 
> You could have saved yourself a lot trouble by using
> 
> ```
> puts "hashkey value is #{rubyhash["hashkey"]}"
> 
> ```
> 
> Regards, Morton

I am learning Ruby step by step. There appear to be so many different  
ways of achieving the same thing, coming from a more procedural  
background, there are quite a number of things about the Ruby way I have  
to learn.

As you can see from here

> **[DEVShed](https://devshed.com)**
>
> Founded in 1997, DEVShed is the perfect place for web developers to learn, share their work, and build upon the ideas of others.

I have an even more troublesome question I can’t locate. I will post it  
when I find it.

---

<div class="post-metadata">

**Author:** ![vfcforums](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/85e7bf/32.png) [@vfcforums](https://www.ruby-forum.com/u/vfcforums)\
**Post date:** [June 26, 2007, 6:51pm UTC](https://www.ruby-forum.com/t/some-help-needed-with-understanding-ruby-hashes/107773/7 "2007-06-26T18:51:00Z")

</div>

On Jun 26, 2007, at 7:45 AM, Frank C. wrote:

> have  
> to learn.

The point to note about ‘puts “hashkey value is #{rubyhash  
[“hashkey”]}”’ is that the interpreter calls to\_s on the result of a #  
{…} evaluation, but String#+ is picky about is argument – the  
argument must be a string. That means, as you have found out, that  
you must explicitly coerce anything not a string with to\_s before  
passing it to String#+.

Regards, Morton

---

<div class="post-metadata">

**Author:** ![vfcforums](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/85e7bf/32.png) [@vfcforums](https://www.ruby-forum.com/u/vfcforums)\
**Post date:** [June 26, 2007, 12:56pm UTC](https://www.ruby-forum.com/t/some-help-needed-with-understanding-ruby-hashes/107773/8 "2007-06-26T12:56:12Z")

</div>

On Jun 26, 2007, at 5:27 AM, Frank C. wrote:

> I tracked the error, I was testing the using the concatenating strings  
> without using to\_s in the code. ie.
> 
> puts "hashkey value is " + rubyhash[“hashkey”]
> 
> instead of
> 
> "hashkey value is " + (rubyhash[“hashkey”]).to\_s

You could have saved yourself a lot trouble by using

```
puts "hashkey value is #{rubyhash["hashkey"]}"

```

Regards, Morton
