# Using variables for the hash name and key name

**URL:** <https://www.ruby-forum.com/t/using-variables-for-the-hash-name-and-key-name/103735>\
**Category:** Ruby\
**Created:** [May 9, 2007, 7:59pm UTC](https://www.ruby-forum.com/t/using-variables-for-the-hash-name-and-key-name/103735 "2007-05-09T19:59:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sheepfunk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f1d935/32.png) [@sheepfunk](https://www.ruby-forum.com/u/sheepfunk)\
**Post date:** [May 9, 2007, 7:59pm UTC](https://www.ruby-forum.com/t/using-variables-for-the-hash-name-and-key-name/103735/1 "2007-05-09T19:59:32Z")

</div>

I’m trying to call a value from a hash using variables for both the hash  
name and the key I’m trying to call

So I’ve got a hash like this

hash\_name = {  
“key\_name\_1” =\> “value\_name\_1”,  
“key\_name\_2” =\> “value\_name\_2”,  
“key\_name\_3” =\> “value\_name\_3”,  
}

and earlier in the script I have two variables:

@hash\_name\_var, which looks like it’s equal to hash\_name

(this statement is true, ‘hash\_name’ == @hash\_name\_var)

@key\_name\_var, which looks like it’s equal to key\_name\_1

(this statement is true, ‘key\_name\_1’ == @key\_name\_var)

So, I’m trying to get the value out of the hash with

@hash\_name\_var[@key\_name\_var]

but this doesn’t work

However, hash\_name[@key\_name\_var] does work, so since  
‘hash\_name’ == @hash\_name\_var it makes me think I need to find  
a way to make:

@hash\_name\_var == hash\_name

instead of

@hash\_name\_var == ‘hash\_name’

---

<div class="post-metadata">

**Author:** ![sheepfunk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f1d935/32.png) [@sheepfunk](https://www.ruby-forum.com/u/sheepfunk)\
**Post date:** [May 9, 2007, 8:57pm UTC](https://www.ruby-forum.com/t/using-variables-for-the-hash-name-and-key-name/103735/2 "2007-05-09T20:57:37Z")

</div>

On May 9, 2007, at 1:59 PM, Skye Weir-Mathews wrote:

> So, I’m trying to get the value out of the hash with
> 
> @hash\_name\_var[@key\_name\_var]

The short answer is “don’t do that”.

If you need to select a particular hash dynamically, then  
simply use another hash as an index:

master = { ‘hash1’ =\> { ‘a’ =\> 1 },  
‘hash2’ =\> { ‘a’ =\> 2, ‘b’ =\> 3}  
}

puts master[‘hash1’][‘a’] # =\> 1  
puts master[‘hash2’][‘b’] # =\> 3

Of course you can use an instance variable to  
select a hash:

@hash\_name = ‘hash1’  
puts master[@hash\_name].inspect # {‘a’ =\> 1}

There are other possibilities. If you are only selecting between  
a couple hashes:

hash = case @select\_hash  
when ‘hash1’ then @hash1  
when ‘hash2’ then @hash2  
end

```
     puts hash.inspect # this will be @hash1 or @hash2

```

If for some reason these technique doesn’t work you’ll have  
to start playing around with eval to get the indirection  
you are looking for.

IMHO, the question you asked generally comes from Perl programmers  
who are trying to emulate Perl’s reference data type in Ruby. That  
is almost always the wrong approach in Ruby.

Gary W.

---

<div class="post-metadata">

**Author:** ![sheepfunk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f1d935/32.png) [@sheepfunk](https://www.ruby-forum.com/u/sheepfunk)\
**Post date:** [May 9, 2007, 10:58pm UTC](https://www.ruby-forum.com/t/using-variables-for-the-hash-name-and-key-name/103735/3 "2007-05-09T22:58:52Z")

</div>

On Thu, May 10, 2007 at 03:56:15AM +0900, Gary W. wrote:

> @hash\_name = ‘hash1’  
> puts hash.inspect # this will be @hash1 or @hash2
> 
> If for some reason these technique doesn’t work you’ll have  
> to start playing around with eval to get the indirection  
> you are looking for.

Or if the values are in instance variables, you can use  
instance\_variable\_get.

@a = “one”  
@b = “two”  
which = “@a”  
puts instance\_variable\_get(which)
