On Fri, Apr 1, 2011 at 11:26 PM, Simon Harrison <simon / simonharrison.net> w=
rote:
> This works perfectly for me now. In case anyone may benefit from it:
>
>
> require 'csv'
>
> def load_xvid_file(path_to_csv)
> =A0@films =3D []
> =A0csv_contents =3D CSV.read(path_to_csv)
> =A0csv_contents.shift
> =A0csv_contents.each do |row|
> =A0 =A0@films << row[0]
> =A0end
> =A0 / films.each { |f| f.downcase! }
> end
>
> def search_for_film
> =A0print "Enter name of film to search for: "
> =A0film =3D gets.chomp.downcase
>
> =A0results =3D [@films.grep(/#{film}/)]
> =A0if 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 =3D @films.grep(/#{film}/)
if results.empty?
  puts "nothing found"
else
  results.each ...


if results
> =A0 =A0results.each { |f| puts f }
> =A0 =A0prompt
> =A0else
> =A0 =A0puts "Nothing found."
> =A0 =A0prompt
> =A0end
> end
>
> def prompt
> =A0print "Search again? (y or n) "
> =A0answer =3D gets.chomp.downcase
>
> =A0case answer
> =A0when /^y/
> =A0 =A0search_for_film
> =A0when /^n/
> =A0 =A0puts "Goodbye."
> =A0 =A0exit
> =A0else
> =A0 =A0prompt
> =A0end
> 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.