Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8749577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:43:03+00:00 2026-06-13T12:43:03+00:00

I want to make a filter class in which I have three methods: name

  • 0

I want to make a filter class in which I have three methods: name, genre, and year. I want to filter a list of movies, in which every object has a name, title and a year, on multiple criteria.

I can’t figure out how to do this. For example:

Filter.name("Batman") & Filter.genre("Drama") | !Filter.year(2001)

I tried to keep the filter fields in a list [name => batman, genre=>drama], and so on, but the operators (especially or (|)) are bugging me.

I’m accepting any ideas.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T12:43:04+00:00Added an answer on June 13, 2026 at 12:43 pm

    The functionality you want fits naturally into Enumerable’s select, which Array inherits.

    Because you’re talking about a list, AKA, array, it’s a natural fit to use select to determine whether your movies meet your criteria by building upon that, instead of passing them into a method.

    A separate Filter class will not fit the Ruby-way as well. Consider sub-classing Array for your movie list, and adding a filter method that takes your criteria as parameters.

    Here’s how I’d write that functionality, but doing it in a way I consider more Ruby-like:

    require 'pp'
    
    class Movies < Array
    
      def initialize(*movies)
        super(movies)
      end
    
      def filter(criteria={})
        self.select{ |m| 
          criteria.all?{ |c, v| 
            m.send(c, v) 
          }
        }
      end
    end
    
    class Movie
    
      def initialize(attributes={})
        @movie = attributes
      end
    
      def name(t)
        case t
        when Regexp
          @movie[:name][t]
        when String
          @movie[:name] == t
        end
      end
    
      def genre(g)
        @movie[:genre] == g
      end
    
      def year(y)
        @movie[:year] == y
      end
    
    end
    
    movies = Movies.new(
      Movie.new(name: 'Batman', genre: 'Drama', year: 2001),
      Movie.new(name: 'The Shawshank Redemption', genre: 'Drama', year: 1994),
      Movie.new(name: 'Raiders of the Lost Ark', genre: 'Action', year: 1981)
    )
    
    pp movies.filter(year: 1981)
    pp movies.filter(genre: 'Drama', year: 2001)
    pp movies.filter(name: /redemption/i)
    

    I used a hash for the Movie class, because it’s quicker. It’s also a dirtier way to go, but it works.

    Here’s the output of running the code:

    [#<Movie:0x007ff7b191ffb8
      @movie={:name=>"Raiders of the Lost Ark", :genre=>"Action", :year=>1981}>]
    [#<Movie:0x007ff7b1920260
      @movie={:name=>"Batman", :genre=>"Drama", :year=>2001}>]
    [#<Movie:0x007ff7b19200d0
      @movie={:name=>"The Shawshank Redemption", :genre=>"Drama", :year=>1994}>]
    

    The actual flow of filtering the movies fits into how we normally write Ruby code. It could be done even better, but that’s a start.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have style sheet with a class name changebackgroundcolor i want make change in
I want to make an application with plugins that also can have plugins (which
I want make a bash script which returns the position of an element from
I am traversing a HTML document using javascript DOM. I want make a list
I have link for example domain.com/de/controler/action?param=value and I want make actionlink to keep same
I have uploaded a app in production mode to linode. I want make changes
I have a model 'Organization' which has following fields class Organization(models.Model): members = models.ManyToManyField(User,related_name='org_members')
I have such model and query class Employer(Models.model) name = ... class JobTitle(Models.model) name
I have a list of Person s each which have multiple fields that I
Anyone want to help make me a better programmer? I have an application where

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.