Skip to content

Instantly share code, notes, and snippets.

@Casual3498
Last active Dec 14, 2017
Embed
What would you like to do?
3lesson
#!/usr/bin/env ruby
filename = 'movies.txt'
keys_array = %i[href name year country date genres duration rank director actors]
films_array =[]
def output_films (arr)
arr.each do |film|
puts "#{film[:name]} :(#{film[:date]} ; #{film[:genres]}) - #{film[:duration]}"
puts "-----------"
end
end
#check if exists command line parameter
filename = ARGV[0] || filename
#check if exists file
unless File.exist?(filename)
puts "File #{filename} not found!"
return
end
#parse file and fill films_array
File.open(filename) do |file|
films_array = file.map { |string| keys_array.zip(string.split('|')).to_h }
end
puts "5 longest films:"
puts "----------------"
output_films films_array.sort_by { |x| x[:duration][0..-3].to_f }.reverse.take(5)
puts "\n\n"
puts "10 comedys by date:"
puts "----------------_"
output_films films_array.select { |x| x[:genres].include?('Comedy') }.sort_by {|x| x[:date] }.take(10)
puts "\n\n"
puts "All directors:"
puts "----------------"
puts films_array.map { |elem| elem[:director] }.uniq.sort_by { |x| x.split(' ').last }
puts "\n\n"
puts "non USA films count: "
puts films_array.count { |film| !film[:country].include?('USA') }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment