# Rails array.each returning array?

**URL:** <https://www.ruby-forum.com/t/rails-array-each-returning-array/161605>\
**Category:** Rails\
**Created:** [February 27, 2009, 7:30pm UTC](https://www.ruby-forum.com/t/rails-array-each-returning-array/161605 "2009-02-27T19:30:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![spacechurro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d2c977/32.png) [@spacechurro](https://www.ruby-forum.com/u/spacechurro)\
**Post date:** [February 27, 2009, 7:30pm UTC](https://www.ruby-forum.com/t/rails-array-each-returning-array/161605/1 "2009-02-27T19:30:48Z")

</div>

i am having a problem with a current piece of code, and it seems like  
the result for each on an array might be the culprit.

according to everything i have seen before, this should work:

a = [“a”, “b”, “c”]  
a.each {|x| print x, " – " }

should produce:

a – b – c –

but i am getting:

> > a = [“a”, “b”, “c”]  
> > =\> [“a”, “b”, “c”]
> 
> > a.each {|x| print x, " – " }  
> > a – b – c – =\> [“a”, “b”, “c”]

does anyone know where the =\> [“a”, “b”, “c”] is coming from?

specifically, i am trying to run:

def self.show\_pending\_users  
self.find(:all, :conditions =\> “pending\_role is not null”).each {  
|x| “#{x.login}” }  
end

where i expect to get a an array of x.login back… but i am getting back  
an array of all the objects …

any ideas?

thanks!

---

<div class="post-metadata">

**Author:** ![spacechurro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d2c977/32.png) [@spacechurro](https://www.ruby-forum.com/u/spacechurro)\
**Post date:** [February 27, 2009, 7:36pm UTC](https://www.ruby-forum.com/t/rails-array-each-returning-array/161605/2 "2009-02-27T19:36:23Z")

</div>

> does anyone know where the =\> [“a”, “b”, “c”] is coming from?

It’s only doing that in the console, because the return value of each is  
the  
array you passed it. If you run this code outside of irb, you won’t see  
it.

SH

---

<div class="post-metadata">

**Author:** ![spacechurro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d2c977/32.png) [@spacechurro](https://www.ruby-forum.com/u/spacechurro)\
**Post date:** [February 27, 2009, 7:37pm UTC](https://www.ruby-forum.com/t/rails-array-each-returning-array/161605/3 "2009-02-27T19:37:01Z")

</div>

What you want is map, not each (the ruby-doc is always your friend):

def self.show\_pending\_users  
self.find(:all, :conditions =\> “pending\_role is not null”).map {|x|  
“#{x.login}” }  
end

- 

Maurício Linhares  
[http://alinhavado.wordpress.com/](http://alinhavado.wordpress.com/) (pt-br) | [http://blog.codevader.com/](http://blog.codevader.com/)  
(en)

On Fri, Feb 27, 2009 at 3:30 PM, Sergio R.

---

<div class="post-metadata">

**Author:** ![spacechurro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d2c977/32.png) [@spacechurro](https://www.ruby-forum.com/u/spacechurro)\
**Post date:** [February 27, 2009, 7:52pm UTC](https://www.ruby-forum.com/t/rails-array-each-returning-array/161605/4 "2009-02-27T19:52:27Z")

</div>

MaurÃ­cio Linhares wrote:

> What you want is map, not each (the ruby-doc is always your friend):

ah!

this ended up working perfectly…

thanks so much everyone…
