From: John Carter Date: 2005-04-20T12:43:00+09:00 Subject: Lookup, a.collect{|i|b[i]} # Rubyable email.. #I suspect there is a standard trick for this, but can't spot it now... a = [3,6,3,1,6] # Arbitary Array of stuff b = [1,2,3,4,5,6,7,8,9,0] # Could be Array or Hash, don't care. p a.collect{|i|b[i]} # Outputs... #[4, 7, 4, 2, 7] # I vaguely feel that there should be a standard method "lookup" and # perhaps a standard module Indexable. # Like module Enumerable assumes there exists an "each" method, # module Indexable assume it's including class has an [] method. module Indexable def lookup(*ary) ary.collect{|i| self[i]} end end class Array include Indexable end class Hash include Indexable end p b.lookup( 3,6,3,1,6) # outputs... #[4, 7, 4, 2, 7] h = {'tom'=>1, 'dick'=>2, 'harry'=>3} p h.lookup(*%w{tom tom dick harry dick}) # Outputs... #[1, 1, 2, 3, 2] #John Carter Phone : (64)(3) 358 6639 #Tait Electronics Fax : (64)(3) 359 4632 #PO Box 1645 Christchurch Email : john.carter@tait.co.nz #New Zealand #Refactorers do it a little better every time.