# Array of hashes issue

**URL:** <https://www.ruby-forum.com/t/array-of-hashes-issue/175747>\
**Category:** Ruby\
**Created:** [September 18, 2009, 10:13pm UTC](https://www.ruby-forum.com/t/array-of-hashes-issue/175747 "2009-09-18T22:13:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bdaily](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ed8c4c/32.png) [@bdaily](https://www.ruby-forum.com/u/bdaily)\
**Post date:** [September 18, 2009, 10:13pm UTC](https://www.ruby-forum.com/t/array-of-hashes-issue/175747/1 "2009-09-18T22:13:08Z")

</div>

Hi,

I’m new to ruby and am trying to do a project so I can learn ruby and  
build a tool I need for my job.

I want to creat an array of hashes and then get the information out.

This is what I have right now:

this is in my getIssuesInFileter class:  
@issues = @jira.getIssuesFromFilter(@key)  
@output = Array.new(@issues.length, Hash.new)  
@issues.each {  
|fl|  
@output \<\< {:id =\> fl.id, :assignee =\> fl.assignee, :description  
=\> fl.description, :status =\> fl.status, :type =\> fl.type, :updated =\>  
fl.updated}  
}  
return @output

This is where I want to output my results  
issueArray = @JiraClass.getIssuesInFilter(newFilterVal[0])  
issueArray.each {  
|iss|  
iss.each {  
|issSecD|  
puts issSecD[:description], “\n”  
}  
}

I’ve tried to set up the hash like this {‘id’ =\> fl.id, …

and then address it like this puts issSecD[‘id’], “\n”

But, I keep getting errors. Tried to find answers on google. Got  
frustrated so I figured I’d ask the board.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![bdaily](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ed8c4c/32.png) [@bdaily](https://www.ruby-forum.com/u/bdaily)\
**Post date:** [September 18, 2009, 11:07pm UTC](https://www.ruby-forum.com/t/array-of-hashes-issue/175747/2 "2009-09-18T23:07:30Z")

</div>

On Fri, Sep 18, 2009 at 10:13 PM, Brad D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I’m new to ruby and am trying to do a project so I can learn ruby and  
> build a tool I need for my job.

Hi, welcome !

> I want to creat an array of hashes and then get the information out.
> 
> This is what I have right now:
> 
> this is in my getIssuesInFileter class:  
> @issues = @jira.getIssuesFromFilter(@key)  
> @output = Array.new(@issues.length, Hash.new)

First thing: I think this is incorrect, because you start with a  
filled array of length @issues.length, all positions pointing to an  
empty Hash. Check this:

irb(main):001:0\> a = Array.new(10, Hash.new)  
=\> [{}, {}, {}, {}, {}, {}, {}, {}, {}, {}]  
irb(main):002:0\> a \<\< {:a =\> 3}  
=\> [{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {:a=\>3}]

You see, when I add to the array, I still have 10 positions pointing  
to an empty hash. Don’t think this is what you want.  
I would do just:

@output = [] # or Array.new

> ```
> @issues.each {
> |fl|
> @output << {:id => fl.id, :assignee => fl.assignee, :description
> 
> ```
> 
> =\> fl.description, :status =\> fl.status, :type =\> fl.type, :updated =\>  
> fl.updated}  
> }  
> return @output

When things don’t go as expected, you can inspect the variables you  
are dealing with, to see what’s going on:

> This is where I want to output my results  
> issueArray = @JiraClass.getIssuesInFilter(newFilterVal[0])  
> issueArray.each {  
> |iss|  
> p iss  
> iss.each {  
> |issSecD|  
> p issSecD  
> puts issSecD[:description], “\n”  
> }  
> }

Adding those two lines should give you a clue of what’s going on: iss  
is one of the hashes in the array. issSecD is an array with each  
[key,value]. If you are only interested in the description:

issueArray = @JiraClass.getIssuesInFilter(newFilterVal[0])  
issueArray.each do |issue|  
puts issue[:description] #puts already prints a “\n”. don’t know if  
you actually want 2 of them.  
end

If you want to iterate all of the keys:

issueArray = @JiraClass.getIssuesInFilter(newFilterVal[0])  
issueArray.each do |issue|  
issue.each do |key, value|  
puts “#{key} =\> #{value}”  
end

Hope this helps,

Jesus.

---

<div class="post-metadata">

**Author:** ![bdaily](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ed8c4c/32.png) [@bdaily](https://www.ruby-forum.com/u/bdaily)\
**Post date:** [September 19, 2009, 10:54pm UTC](https://www.ruby-forum.com/t/array-of-hashes-issue/175747/3 "2009-09-19T22:54:31Z")

</div>

On Sat, Sep 19, 2009 at 12:54 AM, Brad D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > > ï¿½ ï¿½ ï¿½}  
> > > p iss  
> > > [key,value]. If you are only interested in the description:  
> > > issueArray.each do |issue|  
> > > Hi,
> 
> This was very helpful and worked perfectly for me. But, I’m still not  
> there on what I want to do. I should have explained better. So, now that  
> I have a good hash with my information into it I want to pass it to  
> another method. If I were doing this in PHP or something it would look  
> like this:
> 
> myFunction(issueHash[‘id’], issueHash[‘description’],…)
> 
> So I guess what I’m missing is how do get my values out using the key?

I think you are setting the hash with symbols as keys, so you retrieve  
them passing the hash the appropriate key:

issueHash[:id]  
issueHash[:description]  
…

(I’m not sure if this is the problem you are facing right now)

Jesus.

---

<div class="post-metadata">

**Author:** ![bdaily](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ed8c4c/32.png) [@bdaily](https://www.ruby-forum.com/u/bdaily)\
**Post date:** [September 19, 2009, 12:54am UTC](https://www.ruby-forum.com/t/array-of-hashes-issue/175747/4 "2009-09-19T00:54:07Z")

</div>

JesÃºs Gabriel y GalÃ¡n wrote:

> @output = [] # or Array.new
> 
> > ï¿½ ï¿½ ï¿½@issues.each {  
> > ï¿½ ï¿½ ï¿½ ï¿½|fl|  
> > ï¿½ ï¿½ ï¿½ ï¿½@output \<\< {:id =\> fl.id, :assignee =\> fl.assignee, :description  
> > =\> fl.description, :status =\> fl.status, :type =\> fl.type, :updated =\>  
> > fl.updated}  
> > ï¿½ ï¿½ ï¿½}  
> > return @output
> 
> When things don’t go as expected, you can inspect the variables you  
> are dealing with, to see what’s going on:
> 
> > This is where I want to output my results  
> > ï¿½ ï¿½ ï¿½ ï¿½issueArray = @JiraClass.getIssuesInFilter(newFilterVal[0])  
> > ï¿½ ï¿½ ï¿½issueArray.each {  
> > ï¿½ ï¿½ ï¿½ ï¿½|iss|  
> > p iss  
> > ï¿½ ï¿½ ï¿½ ï¿½iss.each {  
> > ï¿½ ï¿½ ï¿½ ï¿½ ï¿½|issSecD|  
> > p issSecD  
> > ï¿½ ï¿½ ï¿½ ï¿½ ï¿½puts issSecD[:description], “\n”  
> > ï¿½ ï¿½ ï¿½ ï¿½}  
> > ï¿½ ï¿½ ï¿½}
> 
> Adding those two lines should give you a clue of what’s going on: iss  
> is one of the hashes in the array. issSecD is an array with each  
> [key,value]. If you are only interested in the description:
> 
> issueArray = @JiraClass.getIssuesInFilter(newFilterVal[0])  
> issueArray.each do |issue|  
> puts issue[:description] #puts already prints a “\n”. don’t know if  
> you actually want 2 of them.  
> end
> 
> If you want to iterate all of the keys:
> 
> issueArray = @JiraClass.getIssuesInFilter(newFilterVal[0])  
> issueArray.each do |issue|  
> issue.each do |key, value|  
> puts “#{key} =\> #{value}”  
> end
> 
> Hope this helps,
> 
> Jesus.

Hi,

This was very helpful and worked perfectly for me. But, I’m still not  
there on what I want to do. I should have explained better. So, now that  
I have a good hash with my information into it I want to pass it to  
another method. If I were doing this in PHP or something it would look  
like this:

myFunction(issueHash[‘id’], issueHash[‘description’],…)

So I guess what I’m missing is how do get my values out using the key?

---

<div class="post-metadata">

**Author:** ![bdaily](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ed8c4c/32.png) [@bdaily](https://www.ruby-forum.com/u/bdaily)\
**Post date:** [September 23, 2009, 3:54am UTC](https://www.ruby-forum.com/t/array-of-hashes-issue/175747/5 "2009-09-23T03:54:21Z")

</div>

> Wish there was an easier way to get a value out of a hash by key.  
> Someone on the board might know.
> 
> Getting values out of a hash by key is easy. I think the issue you were  
> having is you were unclear exactly what the key was and maybe were  
> confused  
> about the difference between a Symbol and String in Ruby. A Symbol is  
> kind  
> of like an interned String (to borrow some Java speak). They are  
> separate  
> classes and both may be used as hash keys. So this works:

h = {}  
h[:key] = ‘my key is a Symbol’  
h[‘key’] = ‘my key is a String’

p h[:key]  
“my key is a Symbol”  
p h[‘key’]  
“my key is a String”

Getting a value out of the hash is as simple as it is in PHP – just use  
the  
[] method.

-Doug Seifert

---

<div class="post-metadata">

**Author:** ![bdaily](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ed8c4c/32.png) [@bdaily](https://www.ruby-forum.com/u/bdaily)\
**Post date:** [September 23, 2009, 1:49am UTC](https://www.ruby-forum.com/t/array-of-hashes-issue/175747/6 "2009-09-23T01:49:33Z")

</div>

Hey Jesus,

You led me in the right direction. Thanks!

I ended up having to do it like this:

issue.fetch(“description”)

Wish there was an easier way to get a value out of a hash by key.  
Someone on the board might know.

---

<div class="post-metadata">

**Author:** ![bdaily](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ed8c4c/32.png) [@bdaily](https://www.ruby-forum.com/u/bdaily)\
**Post date:** [September 23, 2009, 8:27pm UTC](https://www.ruby-forum.com/t/array-of-hashes-issue/175747/7 "2009-09-23T20:27:15Z")

</div>

> h = {}  
> h[:key] = ‘my key is a Symbol’  
> h[‘key’] = ‘my key is a String’
> 
> p h[:key]  
> “my key is a Symbol”  
> p h[‘key’]  
> “my key is a String”
> 
> Getting a value out of the hash is as simple as it is in PHP – just use  
> the  
> [] method.
> 
> -Doug Seifert

Thanks Doug! I’ll give it a shot. Looks easy enough. You’re right, I  
didn’t get the difference between symbol and string.
