Skip to content

Instantly share code, notes, and snippets.

@Nursultan91
Created March 6, 2018 09:58
Show Gist options
  • Save Nursultan91/a9f5aa80b64dee55f1bbd8ea26f2d0d4 to your computer and use it in GitHub Desktop.
Save Nursultan91/a9f5aa80b64dee55f1bbd8ea26f2d0d4 to your computer and use it in GitHub Desktop.
require 'csv'
require 'ostruct'
require 'date'
filename = ARGV.first || 'movies.txt'
abort("Файл не найден") unless File.exist?(filename)
FIELDS = %i[link title year country premiere genre duration rank director cast]
ostruct_movie_arr = CSV.foreach(filename, col_sep: '|', headers: FIELDS )
.map { |line| OpenStruct.new(line.to_h) }
puts "Статистика по месяцам"
ostruct_movie_arr
.reject{ |movie| movie.premiere.length <= 5 }
.map{|movie| Date.strptime(movie.premiere, '%Y-%m').mon}
.each_with_object(Hash.new(0)){ |month, count| count[month] +=1 }
.sort.map.each {|month, count| puts "#{Date::MONTHNAMES[month]} - #{count}"}
def movie_print(array_for_print)
array_for_print.each { |movie| puts "#{movie.title} (#{movie.premiere}/ #{movie.genre}) - #{movie.duration}" }
end
sorted_ostruct_movie_arr = ostruct_movie_arr.sort_by { |movie| [movie.duration.to_i] }
puts "Пять самых длинных фильмов"
movie_print(sorted_ostruct_movie_arr.last(5))
comedy_ostruct_movie_arr = ostruct_movie_arr.select {|movie| movie.genre.include? "Comedy"}
.sort_by(&:premiere)
puts "Десять самых старых комедий"
movie_print(comedy_ostruct_movie_arr.first(10))
puts "Режиссеры"
puts ostruct_movie_arr
.map {|movie| movie.director}.uniq
.sort_by {|name| name.split(/\W+/).last}
puts "Фильмов снято без участия США #{ostruct_movie_arr.count{|movie| !movie.country.include? "USA"}}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment