Skip to content

Instantly share code, notes, and snippets.

@Prigin
Created November 5, 2019 13:38
Show Gist options
  • Save Prigin/c499894107d7805af0d7dbc01c48295b to your computer and use it in GitHub Desktop.
Save Prigin/c499894107d7805af0d7dbc01c48295b to your computer and use it in GitHub Desktop.
MKDEV
require_relative 'movie_coll.rb'
def filter(unit, mas)
unit = unit.to_a.map do |u|
if u[1].is_a? Array
[u[0], u[1].sort]
else
u
end
end
raise "<filter> error" unless unit.map{|u| mas[0].respond_to?(u[0])}.reduce(true,:&)
buff = mas.select do |m|
unit.inject(true) do |bool, u|
if u[1].is_a? Array
c = [m.send(u[0])]
bool & !( c & u[1]).empty?
elsif u[1].is_a? String
bool & m.send(u[0]).include?(u[1])
elsif u[1].is_a? Integer
bool & (m.send(u[0]) == u[1])
elsif u[1].is_a? Range
bool & (m.send(u[0]) === u[1])
else
false
end
end
end
return buff unless buff.empty?
"no matches"
end
mas = MovieCollection.new("movies_v2.txt")
puts filter({country: "France"}, mas.all).map{|u| "#{u.title} #{u.genre} #{u.country}" }
puts ["Brasil"] & ["Italy", "USA"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment