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

The Archive Base Latest Questions

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

I am trying to make a generic method: class Foo attr_reader def add(object) item

  • 0

I am trying to make a generic method:

class Foo
  attr_reader

  def add(object)
    item = @item.find { |item| item.(#the method is calling object.class) == object if item.is_a?(object.class)}
  end
end

I want to create a generic method which compares the element of an array depending the parameter class.

Example:

item.method1 == method1 if item.is_a?(method1.class) 

where method1 is the name of class.

Also, I need a complete tutorial about metaprogramming and dynamic/generic programming.

  • 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-20T19:50:11+00:00Added an answer on May 20, 2026 at 7:50 pm

    I am unsure of what you are trying to do from your example. As a general rule, in Ruby, you don’t check for types. It is dynamically typed for a reason: you can write code that works for any objects that happen to support the methods your code calls on them.

    From what I can tell from your comments, you want to extend the class Array so, when you call a method on an array like an_array.pack, the array is searched for an instance of Pack and returned. Ruby has a method called whenever a method is found not to exist called Module#method_missing. For example, if I randomly decide to call 4.to_dragon(magic: 4, height: 700), the Ruby interpreter will attempt to find to_dragon as a public method defined on some class or module in the Fixnum (type of numbers) inheritance chain. Provided you have not done something strange to that chain, we get a call to method_missing on the object 4 with these arguments: [:to_dragon, { magic: 4, height: 700 }]. Basically, that’s the name appended to the front of the arguments, and a block should one be given.

    Using this technique, you can override method_missing to get this code as a solution:

    class String
      def method_to_class
        split('_').map(&:capitalize).join
      end
    end
    
    class Array
      def method_missing(method_name, *arguments, &block)
        find do |element|
          element.class.name == method_name.to_s.method_to_class
        end || super
      end
    end
    

    You add a method to String to convert a method name to a class name. Then, you redefine method_missing on Array to check each element to see if the class name matches the given class name. If one is found, then that is returned. Otherwise (and we do this using Ruby’s fancy || operator), the value returned from that function is nil and the second operand to || is returned. This happens to be the default implementation for method_missing (which we get by the super keyword) and returns the error the call deserves.

    The only potential issue with that is, if you have elements that have class names identical to method names that are already defined on arrays, then those will be called instead rather than this special technique. For example, calling an_array.hash will give you the hash code of the array rather than the first instance of a hash.

    A safer technique in this respect is more similar to what I think you were trying to do. It actually uses class objects, and you can use it to override other methods:

    class Array
      def add(class_object)
        class << self
          define_method class_object.name do
            find do |element|
              element.is_a? class_object
            end
          end
        end
      end
    end
    

    This defines new methods directly on an instance of an array. For example:

    an_array = [Array, Hash, Dragon].map &:new
    an_array.add Hash
    an_array.hash    #=> {}
    

    If this answer does not include a solution, it should at least guide you closer!

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

Sidebar

Related Questions

I am trying to make a generic method that can accept any object, in
Trying to make a make generic select control that I can dynamically add elements
I'm learning about generics, so I'm trying to make my own generic array class,
I am trying to make the following method to a generic method and got
Trying to make a generic PL/SQL procedure to export data in specific XML format,
I am trying to make an Outlook 2003 add-in using Visual Studio 2008 on
Trying to make a MySQL-based application support MS SQL, I ran into the following
Trying to make a web service call to an HTTPS endpoint in my Silverlight
I'm trying to make the case for click-once and smart client development but my
I'm trying to make a data bound column invisible after data binding, because it

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.