Morphable: Polymorphisms in Ruby

I’ve been learning the ins and outs of the metaclass. That sounds cool, but it’s badly named: the term singleton class succinctly describes the concept. To prove my Ruby fu, I decided to implement Polymorphisms in Ruby. The hole is this:

class Test
  def a
    puts "Hello World"
  end
  def a(p)
    puts "Hello #{p}"
  end
end

test = Test.new
test.a

…will choke, because unlike C++, Ruby doesn’t understand multiple definitions of functions. It just redefines them, so as soon as the second function a is defined, the first is overwritten. (And since the second definition has a required parameter, changing its arity, the compiler intepreter chokes.)

Enter Morphable. Include this in your class, and methods automatically polymorph depending on their arity. self.included, self.method_added, and method_missing voodoo. There’s no tests (no TDD for me, yet) and no benchmarks (although with the amount of wizardry going on, it’ll be slower). It’ll also for sure break complex meta programming in your own classes 🙂

Refactored:

#!/usr/bin/env ruby

require 'morphable'

class Test
  include Morphable

  def a
    puts("Vishnu Gopal")
  end

  def a(love)
    puts love
  end

  def add
    puts "No numbers given!"
  end

  def add(a, b)
    a + b
  end
end

test = Test.new
test.a
test.a("Love me blind!")
test.add
puts test.add(100, 200)

Download Morphable here. [I’m in the process of setting up a public Subversion/Trac repository. That’ll be available soon].

4 Replies to “Morphable: Polymorphisms in Ruby”

  1. @ sur: Ok, I am new to Ruby, but I do understand the concept of polymorphism. I will have 2 agree with Vish here. The concept of polymorphism is the same function name, having 2 different signatures. Now, that’s not done in your case, as all you are taking is a parameter for the function to do a different thing within its code.

    Advantages – you don’t have to use morphable. C runs on this btw =)

    Disadvantages:
    – The function becomes really complex. Consider the fact that Ruby is being used in major scale web applications. You really don’t want a single function entry point doing a lot of things. Monolithic functions are _just not_ the way to go anymore!

    Like

  2. Hi Vish !!

    I am not completely agreed here. The thing is that every programming language has its own flavor, although i agree that the flavor sometimes need some modifications but if we even hack a bit of rails code then it becomes clear that how ruby(and so rails also) is upto providing same kinda functionality of morphable keeping the DRY in the stream too…. as we can do it like

    class Foo
    def bar(*args)
    puts “hello #{args[0].to_s}”
    end
    end

    or if its that much specific, then we can have it like

    class Foo
    def bar(*args)
    case args[0]
    when nil
    puts “hello something else”
    else
    puts “hello #{args[0].to_s}”
    end
    end
    end

    Thanks.

    Like

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s