class Array def find(pIn, place=nil) z=self[0].class.to_s case z when 'Array' place=0 if not place aSrch=self.sort{|a,b| a[place]<=>b[place]} if place sType='Array' else if place && place.class.to_s=="String" aSrch=instance_eval("self.sort{|a,b| a.#{place}<=>b.#{place}}") sType='Class' else aSrch=self.sort{|a,b| a<=>b} sType='Normal' end end found=false start = 0 stop = aSrch.length while true do if(stop-start) <= 2 aSrch[start..stop].each do |l| case sType when 'Normal' if pIn==l found=self.index(l) break end when 'Array' if pIn==l[place] found=self.index(l) break end when 'Class' if pIn==instance_eval("l.#{place}") found=self.index(l) break end end end return found else pt=(stop-start) / 2 + start case sType when 'Normal' test=aSrch[pt] when 'Array' test=aSrch[pt][place] when 'Class' test=instance_eval("aSrch[#{pt}].#{place}") end if test==pIn found=self.index(aSrch[pt]) break elsif test < pIn start= pt else stop=pt end end end return found end end =begin class Tester attr_reader :val, :name, :place def initialize(val,name,place) @val=val @name=name @place=place end end atest1=[23,10,14,25,6,9,19] atest2=[[23,"Henry","Detroit"],[10,"Josh","Chicago"],[14,"Peter", "San Francisco"],[25,"Ernie", "Atlanta"],[6, "Donna", "Edinburg"],[9,"Shirley", "New York"],[19,"Dumbo", "Milwaukee"]] atest3=[] atest2.each do |row| atest3 << Tester.new(row[0], row[1], row[2]) end z=22 pt=atest1.find(z) if pt puts "found #{z} at #{pt}" else puts "can't find #{z}" end z="zelda" pt=atest2.find(z,1) if pt puts atest2[pt].inspect else puts "can\'t find #{z}" end z="Peter" pt=atest3.find(z, "name") if pt puts "found #{atest3[pt].place}" else puts "Can't find #{z}" end =end