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

  • Home
  • SEARCH
  • 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 71637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:55:08+00:00 2026-05-10T19:55:08+00:00

I have a command line Ruby app I’m developing and I want to allow

  • 0

I have a command line Ruby app I’m developing and I want to allow a user of it to provide code that will run as a filter on part of the process. Basically, the application does this:

  1. read in some data
  2. If a filter is specified, use it to filter data
  3. process the data

I want the filtering process (step 2) to be as flexible as possible.

My thinking was that the user could provide a Ruby file that set a known constant to point to an object implementing an interface I define, e.g.:

# user's filter  class MyFilter   def do_filter(array_to_filter)     filtered_array = Array.new     # do my filtering on array_to_filter     filtered_array end   FILTER = MyFilter.new 

My app’s code would then do something like this:

array_that_might_get_filtered = get_my_array() if (options.filter_file)   require options.filter_file   array_that_might_get_filtered = FILTER.do_filter(array_that_might_get_filtered) end 

While this would work, it feels cheesy and it seems like there should be a better way to do it. I also considered having the filter be in the form of adding a method of a known name to a known class, but that didn’t seem quite right, either.

Is there a better idiom in Ruby for this?

  • 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. 2026-05-10T19:55:09+00:00Added an answer on May 10, 2026 at 7:55 pm

    I’d just use a combination of the command line, and convention.

    If a filter is specified, use it to filter data

    I’m assuming you’d specify a filter on the command line? So you’d invoke the application like this?

    ruby dataprocessor.rb custom_filter 

    If so, you could define an ‘api’ wherein a class name would have to match what was passed in – pretty much exactly how you’ve described in your example.

    To take it one step further though, you could have some logic which looked for the CustomFilter class using ruby’s defined?, and if it was not found, go looking for custom_filter.rb (or any suitable variations) and attempt to load that file, then retry.

    This gives you great extensibility, as you can write as many filter classes as you like, chuck them in their own .rb files, and put them anywhere that ruby can find them. You won’t have to have an pre-defined constants either, the only constraints will be

    1. The class name must match (a variant of) the file name – This is convention in ruby so you’re probably already doing it anyway.
    2. it must have some predefined method, such as your do_filter method

    Incidentally, this is pretty similar to what rails does for requiring your models, and is why you can just use SomeModel without having to always do require app/models/some_model first :-)`

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

Sidebar

Ask A Question

Stats

  • Questions 226k
  • Answers 226k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The DigSig kernel module implements verification of binaries signed by… May 13, 2026 at 1:11 am
  • Editorial Team
    Editorial Team added an answer You can also set the bindings of each column in… May 13, 2026 at 1:11 am
  • Editorial Team
    Editorial Team added an answer You could use under each var name = $(this).parents('tr:first').find('td:first').text(); this… May 13, 2026 at 1:11 am

Related Questions

I have a command line Ruby app I'm developing and I want to allow
I have a Ruby script in my Rails app that I use to load
I am trying to create a simple Automator droplet that will take the style.less
I have a cron job on an Ubuntu Hardy VPS that only half works
I'd like to be able to write a .swf file that is runnable as

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.