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 6195899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:33:02+00:00 2026-05-24T03:33:02+00:00

[ruby 1.8] Assume I have: dummy string do puts thing end Now, this is

  • 0

[ruby 1.8]

Assume I have:

dummy "string" do
    puts "thing" 
end

Now, this is a call to a method which has as input arguments one string and one block. Nice.

Now assume I can have a lot of similar calls (different method names, same arguments). Example:

otherdummy "string" do
    puts "thing"
end

Now because they do the same thing, and they can be hundreds, I don’t want create an instance method for each one in the wanted class. I would like rather find a smart way to define the method dynamically at runtime based on a general rule.

Is that possible? Which techniques are commonly used?

Thanks

  • 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-05-24T03:33:03+00:00Added an answer on May 24, 2026 at 3:33 am

    I’m particularly fond of using method_missing, especially when the code you want to use is very similar across the various method calls. Here’s an example from this site – whenever somebody calls x.boo and boo doesn’t exist, method_missing is called with boo, the arguments to boo, and (optionally) a block:

    class ActiveRecord::Base
      def method_missing(meth, *args, &block)
        if meth.to_s =~ /^find_by_(.+)$/
          run_find_by_method($1, *args, &block)
        else
          super # You *must* call super if you don't handle the
                # method, otherwise you'll mess up Ruby's method
                # lookup.
        end
      end
    
      def run_find_by_method(attrs, *args, &block)
        # Make an array of attribute names
        attrs = attrs.split('_and_')
    
        # #transpose will zip the two arrays together like so:
        #   [[:a, :b, :c], [1, 2, 3]].transpose
        #   # => [[:a, 1], [:b, 2], [:c, 3]]
        attrs_with_args = [attrs, args].transpose
    
        # Hash[] will take the passed associative array and turn it
        # into a hash like so:
        #   Hash[[[:a, 2], [:b, 4]]] # => { :a => 2, :b => 4 }
        conditions = Hash[attrs_with_args]
    
        # #where and #all are new AREL goodness that will find all
        # records matching our conditions
        where(conditions).all
      end
    end
    

    define_method also looks like it would work for you, but I have less experience with it than method_missing. Here’s the example from the same link:

    %w(user email food).each do |meth|
      define_method(meth) { @data[meth.to_sym] }
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is what I have now - which looks too verbose for the work
Ruby has two different exceptions mechanisms: Throw/Catch and Raise/Rescue. Why do we have two?
I have a text like Previously which looks English but has a non-ASCII characters
Say I have a Ruby class, Flight . Flight has an attr_accessor :key on
I have a ruby hash that looks like this { stuff_attributes => { 1
Let's assume we have a web project in which we want to have ~10000
In Ruby, how can I programmatically determine which class/module defines a method being called?
This has to be a common question that all programmers have from time to
Ruby has a wealth of conditional constructs, including if / unless , while /
Ruby on Rails has magic timestamping fields that are automatically updated when a record

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.