Rails array.each returning array?


#1

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!


#2

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


#3

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/ (pt-br) | http://blog.codevader.com/
(en)

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


#4

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…