From: Robert Klemme Date: 2010-03-30T16:19:42+09:00 Subject: Re: Fastest Array Search? 2010/3/30 Derek Cannon : > Hello, everyone! What would be the quickest/best way to check elements > of an array, which themselves are arrays of hashes? Quick example: > > mov1 = {"title"=>"Batman Returns", "genre"=>"Action"} > mov2 = {"title"=>"Batman", "genre"=>"Action"} > mov3 = {"title"=>"Batman Begins", "genre"=>"Action"} > mov4 = {"title"=>"The Dark Knight", "genre"=>"Action"} > array = mov1, mov2, mov3, mov4 > > Right now, to search for "Batman Beings", for instance, I'm using: > > puts array.each { |i| >  if i["title"] == "Batman Begins" >    puts "Found!" >    break # ends the searching because item is found >  end >  } > > Is there a better or faster way to do this in Ruby? I'm going to be > looking through much more data then this, so I want the most efficient > code possible. I'm just a beginner, so this is pretty much all I know > how to do in Ruby. I'd use #find: array.find {|i| i["title"] == "Batman Begins"} If you need to sift through large volumes of data it is reasonable to create an index using one or more Hash instances. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/