Last active
October 23, 2019 13:57
-
-
Save Prigin/83f2a0a79a1b9733a3d066765883f612 to your computer and use it in GitHub Desktop.
task 5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
load 'movie.rb' | |
class MovieCollection | |
attr_accessor :all | |
def read(unit) | |
@all = File.readlines(unit).map{|line| Movie.new( *line.chomp.split("|") ) } | |
end | |
def sort_by(field) | |
begin | |
return @all.sort_by(&field) | |
rescue | |
return "not an option" | |
end | |
end | |
def filter(field_val) | |
field_val = field_val.to_a.flatten | |
begin | |
return @all.select{|u| u.send(field_val[0]).include?(field_val[1]) } | |
rescue | |
return "not an option" | |
end | |
end | |
def stats(field) | |
begin | |
return @all.flat_map(&field) | |
.each_with_object(Hash.new(0)){|val, stats| stats[val] += 1} | |
.sort | |
rescue | |
"not an option" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment