Extracting one column from an array of objects

This seems really simple but I can’t seem to find an answer.

Say I have an array of Users which has a column called ‘id’. I need an
array that holds just the ids. Right now I’m doing:

for user in users
ids << user.id
end

Is there a method to do this? Note that these objects aren’t from my
database so I can’t use find.

You should be able to use ids= users.collect {|u| u.id}

G.

u can also do like

@ids = User.find(:all, :select => 'id', :order=>'id')

Say I have an array of Users which has a column called ‘id’. I need an
array that holds just the ids.

ids = users.collect(&:id)

users.map { |user| user.id }

Samuel F.

Em 23/07/2008, ¨¤s 01:24, Ro Ma [email protected]
escreveu:
!#x000a

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs