Array of hashes issue

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.

On Fri, Sep 18, 2009 at 10:13 PM, Brad D. [email protected]
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.

On Sat, Sep 19, 2009 at 12:54 AM, Brad D. [email protected]
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.

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?

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

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.

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.