views:

73

answers:

1

I am trying to synchronize members in the db with an external source

My plan was to use the array collection in the active record class and on each update for a member found in the external source, I would remove that member entry in the array(if it exists) then after I am done iterating the external source I would iterate and operate on the members left in the array.

The question is how to find the index for the array with a specified id?

member_queue    Array[2] 
[0] #<Member:0x7181404> 
 @attributes Hash[9] 
  'name' "Steve" 
  'id' "953125641"  
 @attributes_cache Hash[0] 
[1] #<Member:0x717fb68> 
 @attributes Hash[9] 
 @attributes_cache Hash[0]

for example, I would search for id="953125641" and that would return 0 so I could delete that entry from the array

+1  A: 

I'd look at using the index function. It's similar to C++'s find_if function (if that's any help!)

index = member_queue.index {|m| m == "953125641" }
Jeff Paquette
I am getting this error: irb(main):001:0> [1,2,3].index {|x|x==2}ArgumentError: wrong number of arguments (0 for 1) from (irb):1:in `index' from (irb):1
Marcus
It works for me with ruby 1.8.7. What version are you using?
Jeff Paquette
1.8.6 - I am using aptana studio, I guess I'll try to figure out how to updage
Marcus
I updated to 1.9.1 using http://blog.orangecabin.com/2009/05/install-ruby-1-9-on-windows-using-zip-binary/ and it works!
Marcus