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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:35:30+00:00 2026-06-02T06:35:30+00:00

I want to use overloading feature in Ruby like many other languages, but Ruby

  • 0

I want to use overloading feature in Ruby like many other languages, but Ruby itself does not support this feature.

Do I have to implement it using the way that define a method with *args argument and determine the number and types of the arguments inside the method? Some like:

class A
    def foo(*args)
        case (args.length)
        when 1
            do something
        when 2
            do something-else
        ....
        end
    end
end

You can see, it is really ugly than directly overloading.

I want to know whether there is any keywords or some other manners (like a meta-programming module) that could allow me to define an overloading method in a more elegant way.

  • 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-02T06:35:32+00:00Added an answer on June 2, 2026 at 6:35 am

    You could try some meta programming to reach your target.

    See the following code:

    class OverloadError < ArgumentError; end
    class Class
    =begin rdoc
    
    =end
      def define_overload_method( methodname, *methods )    
        methods.each{ | proc |
          define_method("#{methodname}_#{proc.arity}".to_sym, &proc )
        }
        define_method(methodname){|*x|
          if respond_to?("#{methodname}_#{x.size}")
          send "#{methodname}_#{x.size}", *x
          else
            raise OverloadError, "#{methodname} not defined for #{x.size} parameters"
          end
        }
    
      end
    end
    
    class X
      define_overload_method :ometh,
            Proc.new{ "Called me with no parameter" },
            Proc.new{ |p1| "Called me with one parameter (#{p1.inspect})" },
            Proc.new{ |p1,p2| "Called me with two parameter (#{p1.inspect}, #{p2.inspect})" }
    end
    
    x = X.new
    
    p '----------'
    p x.ometh()
    p x.ometh(1)
    p x.ometh(1,2)
    p x.ometh(1,2,3)  #OverloadError
    

    You can define your overloaded method with define_overload_method. Parameters are the method name and a list of procedures. The method methodname is created and calls the corresponding method. Which method is determined by the number of parameters (Not type!).

    An alternative syntax would be:

    class OverloadError < ArgumentError; end
    class Class
      def def_overload( methodname)    
        define_method(methodname){|*x|
          if respond_to?("#{methodname}_#{x.size}")
          send "#{methodname}_#{x.size}", *x
          else
            raise OverloadError, "#{methodname} not defined for #{x.size} parameters"
          end
        }
      end
      def overload_method( methodname, proc )    
        define_method("#{methodname}_#{proc.arity}".to_sym, &proc )
      end
    end
    class X
      def_overload :ometh
      overload_method :ometh, Proc.new{ "Called me with no parameter" }
      overload_method :ometh, Proc.new{ |p1| "Called me with one parameter (#{p1.inspect})" }
      overload_method :ometh, Proc.new{ |p1,p2| "Called me with two parameter (#{p1.inspect}, #{p2.inspect})" }
    end
    

    def_overload defines the frame for your overloaded methods, overload_method defines one ‘overload-method’.

    But as already mentioned by Holger:

    You should try to adapt to the Ruby way. There is a reason why there is no overloading in Ruby. Methods should only do one thing, not magically decide to do vastly different things just because of different arguments. Instead try to take advantage of Duck Typing and if in doubt, use different methods with meaningful names.


    I was curious how I could implement a version with type sensitive overloading. Here it is:

    class OverloadError < ArgumentError; end
    class Class
      def def_overload( methodname)    
        define_method(methodname){|*x|
          methname = "xxx"
          methname = "#{methodname}_#{x.size}#{x.map{|p| p.class.to_s}.join('_')}"
          if respond_to?(methname)
            send methname, *x
          elsif respond_to?("#{methodname}_#{x.size}")
            send "#{methodname}_#{x.size}", *x
          else
            raise OverloadError, "#{methodname} not defined for #{x.size} parameters"
          end
        }
      end
      def overload_method( methodname, *args, &proc )    
        types = []
        args.each{|arg| types << arg.to_s}
        define_method("#{methodname}_#{proc.arity}#{types.join('_')}".to_sym, &proc )
      end
    end
    class X
      def_overload :ometh
      overload_method(:ometh){ "Called me with no parameter" }
      overload_method(:ometh, String ){ |p1| "Called me with one string parameter (#{p1.inspect})" }
      overload_method(:ometh ){ |p1| "Called me with one parameter (#{p1.inspect})" }
      overload_method(:ometh){ |p1,p2| "Called me with two parameter (#{p1.inspect}, #{p2.inspect})" }
    end
    

    When you call it with

    p x.ometh(1)
    p x.ometh('a')
    

    You get

        "Called me with one parameter (1)"
        "Called me with one string parameter (\"a\")"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want use JQuery mobile for the front-end of my mobile application, but I
I want to overload ++ operator to use pre-increment and post-increment using operator overloading
I want to write a file_handle class like this #include <stdio.h> #include <iostream> #include
I'd like to use a map that would be equivalent to ConcurrentMap (I want
Ie. I want to code a class ADT that can be used like this:
I want to use operator overloading with dynamic object in C++ how can i
I want use <p:messages> to display error message, use <p:growl> to display success messages.
I want use groovy findAll with my param to filtering closure filterClosure = {
i want use some data from a website with web service. i have a
I want use jQuery in my project. I know the javascript_include_tag calls the jQuery

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.