Skip to content

Instantly share code, notes, and snippets.

@Prigin
Last active October 23, 2019 13:57
Show Gist options
  • Save Prigin/83f2a0a79a1b9733a3d066765883f612 to your computer and use it in GitHub Desktop.
Save Prigin/83f2a0a79a1b9733a3d066765883f612 to your computer and use it in GitHub Desktop.
task 5
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