Forum: Ruby on Rails rails array.each returning array?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Sergio T. R. (Guest)
on 2009-02-27 20:30
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!
Starr H. (Guest)
on 2009-02-27 20:36
(Received via mailing list)
> 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
Maurício L. (Guest)
on 2009-02-27 20:37
(Received via mailing list)
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.
Sergio T. R. (Guest)
on 2009-02-27 20:52
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...
This topic is locked and can not be replied to.