# Converting an Array to a Hash

**URL:** <https://www.ruby-forum.com/t/converting-an-array-to-a-hash/110385>\
**Category:** Rails\
**Created:** [July 25, 2007, 11:12pm UTC](https://www.ruby-forum.com/t/converting-an-array-to-a-hash/110385 "2007-07-25T23:12:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![newrubygirl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e274bd/32.png) [@newrubygirl](https://www.ruby-forum.com/u/newrubygirl)\
**Post date:** [July 25, 2007, 11:12pm UTC](https://www.ruby-forum.com/t/converting-an-array-to-a-hash/110385/1 "2007-07-25T23:12:27Z")

</div>

Hi.

I currently have an array, but realized that I need a hash instead.

I have this code in my users\_helper.rb

1. def generate\_sub\_nav
2. 

```
sub_nav_array = []

```

3. 

```
  user = User.find(session[:user])

```

4. 

```
  for role in user.roles

```

5. 

```
    for right in role.rights

```

6. 

```
      if right.on_menu

```

7. 

```
        sub_nav_array << right.action

```

8. 

```
      end

```

9. 

```
    end # for right

```

10. 

```
  end # for role

```

11. 

```
 sub_nav_array.sort!

```

12. end # def

This is in application\_helper.rb

1. for menu in menu\_items
2. html\_out \<\< link\_to\_unless\_current (menu,
3. 

```
    :controller => menu,

```

4. :action =\> menu)
5. end

Any help to convert this to create menu links would be appreciated!!!

---

<div class="post-metadata">

**Author:** ![newrubygirl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e274bd/32.png) [@newrubygirl](https://www.ruby-forum.com/u/newrubygirl)\
**Post date:** [July 26, 2007, 1:57pm UTC](https://www.ruby-forum.com/t/converting-an-array-to-a-hash/110385/2 "2007-07-26T13:57:30Z")

</div>

Hi Becca, it looks like this shouldn’t be to bad to convert over…

A hash requires two values for every entry, a key and a value for that  
key,  
keeping this in mind…

1. def generate\_sub\_nav
2. 

```
sub_nav_hash = Hash.new

```

3. 

```
  user = User.find(session[:user])

```

4. 

```
  for role in user.roles

```

5. 

```
    for right in role.rights

```

6. 

```
      if right.on_menu

```

7. 

```
        sub_nav_hash[:Key_Goes_Here] = right.action

```

8. 

```
      end

```

9. 

```
    end # for right

```

10. 

```
  end # for role
   # The below line won't work, Hashes don't sort!, but you 

```

can  
still iterate through them  
11. #sub\_nav\_array.sort!  
12. end # def

1. for menu in menu\_items  
html\_out = Hash.new
2. html\_out[:Key\_Goes\_Here] = link\_to\_unless\_current (menu,
3. 

```
    :controller => menu,

```

4. :action =\> menu)
5. end

That should give you an idea of what you can do there… The nice thing  
about hashes is that if the key you put in place of “:Key\_Goes\_Here”  
doesn’t  
exists it is still treated like it does and assigned a value  
appropriately… The other thing is that when accessing a Hash if a  
Key  
you try to get a value for doesn’t exist then the hash return nil…  
Keep  
those in mind while you code 🙂 Other than that you just have to  
figure  
out what you want your key to be named and you should be good to go…  
since its a loop if you have an associated string or something to go  
with  
that value you could dump that in for the key. Oh, and you don’t have  
to  
use Hash.new you can also just do:

html\_out = {}

And I believe that will get you the same result… Good luck and happy  
coding!

---

<div class="post-metadata">

**Author:** ![newrubygirl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e274bd/32.png) [@newrubygirl](https://www.ruby-forum.com/u/newrubygirl)\
**Post date:** [July 26, 2007, 3:43pm UTC](https://www.ruby-forum.com/t/converting-an-array-to-a-hash/110385/3 "2007-07-26T15:43:44Z")

</div>

Thanks Patrick for your great help!

The reason for converting this from an array to a hash is to get the  
link\_to values that I need in the application\_helper file.

That being said, how can I make the values from the user\_helper which  
include controller name, action and text available to the  
application\_helper?

THANKS!  
Becca

---

<div class="post-metadata">

**Author:** ![newrubygirl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e274bd/32.png) [@newrubygirl](https://www.ruby-forum.com/u/newrubygirl)\
**Post date:** [July 26, 2007, 10:21pm UTC](https://www.ruby-forum.com/t/converting-an-array-to-a-hash/110385/4 "2007-07-26T22:21:28Z")

</div>

Unfortunately, this is where I run into a snag… I haven’t used the  
helper  
files yet as most of my application wide functions just go directly into  
application.rb… The helper files tend to just provide additional  
functionality to the templates from what I’ve read though, so you might  
have  
to move that code into controllers, there might yet be a way to do it  
though, unfortunately I don’t know how ☹
