Overloading in Ruby

there are few gems that provide this feature to your ruby code

  1. functional-ruby
       defn(:greet, :male) {
         puts "Hello, sir!"
       }

       defn(:greet, :female) {
         puts "Hello, ma'am!"
       }

       foo.greet(:male)   => "Hello, sir!"
       foo.greet(:female) => "Hello, ma'am!"

you can find more Elixir like pattern matching features from here

  1. contracts.ruby
       Contract 1 => 1
       def fact x
         x
       end

       Contract C::Num => C::Num
       def fact x
        x * fact(x - 1)
       end

this gem helps to right beautiful defensive code. there are some criticisms about performance. so benchmark and decide. more examples


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\")"

You can test for the existence of each argument separately as they are set to nil if not passed (assuming they are passed in order!).

If you insist on very different arguments I suggest an hash argument with symbols for each argument you intend.. and approriate tests.

** UPDATE **

Also you could also rename the methods that overload with more specific names, such as

def perform_task_with_qualifier_1