From: "Jesús Gabriel y Galán" Date: 2011-04-02T06:45:33+09:00 Subject: Re: Searching a CSV file - beginner seeking help On Fri, Apr 1, 2011 at 11:26 PM, Simon Harrison wrote: > This works perfectly for me now. In case anyone may benefit from it: > > > require 'csv' > > def load_xvid_file(path_to_csv) >  @films = [] >  csv_contents = CSV.read(path_to_csv) >  csv_contents.shift >  csv_contents.each do |row| >    @films << row[0] >  end >  @films.each { |f| f.downcase! } > end > > def search_for_film >  print "Enter name of film to search for: " >  film = gets.chomp.downcase > >  results = [@films.grep(/#{film}/)] >  if results this will always be true, since you are initializing results to an array. Enumerable#grep already returns an array, so I'd do: results = @films.grep(/#{film}/) if results.empty? puts "nothing found" else results.each ... if results >    results.each { |f| puts f } >    prompt >  else >    puts "Nothing found." >    prompt >  end > end > > def prompt >  print "Search again? (y or n) " >  answer = gets.chomp.downcase > >  case answer >  when /^y/ >    search_for_film >  when /^n/ >    puts "Goodbye." >    exit >  else >    prompt >  end > end > > load_xvid_file("/home/simon/Documents/CSV/XviD.csv") > search_for_film By the way, I'd change a bit around the logic and the user interface, you seem to have both a little bit mixed up. I would call method prompt from the main script, in that method I would ask the user for a word and call search_for_film passing what the user typed. From that method I would return the results array. Back in the prompt method, I would print the results and loop for another run. This way, search_for_film is not tied to the specific user interaction and is more general and easier to refactor and reuse. Jesus.