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!
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
> 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Ruiz
<rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
wrote:>
> 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!
> --
> Posted via http://www.ruby-forum.com/.
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
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... -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---