Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overloading in Ruby

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.

like image 238
Shou Ya Avatar asked Apr 14 '12 19:04

Shou Ya


3 Answers

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

like image 104
Oshan Wisumperuma Avatar answered Sep 21 '22 15:09

Oshan Wisumperuma


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\")"
like image 35
knut Avatar answered Sep 21 '22 15:09

knut


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
like image 20
Arth Avatar answered Sep 19 '22 15:09

Arth