# Ruby Newbie - array/hash help

**URL:** <https://www.ruby-forum.com/t/ruby-newbie-array-hash-help/67758>\
**Category:** Ruby\
**Created:** [September 6, 2006, 4:46pm UTC](https://www.ruby-forum.com/t/ruby-newbie-array-hash-help/67758 "2006-09-06T16:46:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aladrin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f8ce5/32.png) [@aladrin](https://www.ruby-forum.com/u/aladrin)\
**Post date:** [September 6, 2006, 4:46pm UTC](https://www.ruby-forum.com/t/ruby-newbie-array-hash-help/67758/1 "2006-09-06T16:46:54Z")

</div>

Given the following code…

my $ds = new DataSources($dbh);  
my $data\_sources = $ds-\>getTableNames();

foreach my $table\_name (keys %{$data\_sources}){  
…some code…  
}

Is there any effective way to reproduce the above foreach loop in Ruby?  
I’ve been doing research, but I can’t make heads or tails of it. Any  
assistance would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![aladrin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f8ce5/32.png) [@aladrin](https://www.ruby-forum.com/u/aladrin)\
**Post date:** [September 6, 2006, 5:30pm UTC](https://www.ruby-forum.com/t/ruby-newbie-array-hash-help/67758/2 "2006-09-06T17:30:11Z")

</div>

William C. wrote:

> Given the following code…
> 
> my $ds = new DataSources($dbh);  
> my $data\_sources = $ds-\>getTableNames();
> 
> foreach my $table\_name (keys %{$data\_sources}){  
> …some code…  
> }
> 
> Is there any effective way to reproduce the above foreach loop in Ruby?  
> I’ve been doing research, but I can’t make heads or tails of it. Any  
> assistance would be greatly appreciated.

Wouldn’t that just be:

data\_sources.each\_key do |table\_name|  
puts table\_name  
end

(I’m not real sure how the whole datasource/tablename thing works in  
ruby, but I was assuming datasources was a Hash once you got it.)

---

<div class="post-metadata">

**Author:** ![aladrin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f8ce5/32.png) [@aladrin](https://www.ruby-forum.com/u/aladrin)\
**Post date:** [September 6, 2006, 6:30pm UTC](https://www.ruby-forum.com/t/ruby-newbie-array-hash-help/67758/3 "2006-09-06T18:30:29Z")

</div>

On Thu, 7 Sep 2006, William C. wrote:

> > Is there any effective way to reproduce the above foreach loop in Ruby?  
> > I’ve been doing research, but I can’t make heads or tails of it. Any  
> > assistance would be greatly appreciated.
> 
> Wouldn’t that just be:
> 
> data\_sources.each\_key do |table\_name|  
> puts table\_name  
> end

puts data\_sources.keys

😉

-a

---

<div class="post-metadata">

**Author:** ![aladrin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f8ce5/32.png) [@aladrin](https://www.ruby-forum.com/u/aladrin)\
**Post date:** [September 6, 2006, 8:08pm UTC](https://www.ruby-forum.com/t/ruby-newbie-array-hash-help/67758/4 "2006-09-06T20:08:06Z")

</div>

unknown wrote:

> puts data\_sources.keys
> 
> 😉
> 
> -a

Heh yeah, but he wanted ‘…some code…’… I just put something in  
there that worked. I assumed he wanted something quite a bit more  
complex than that and that he could figure out that’s where it went 😉
