14

In C#, I can do this:

class Program
{
    static void Main(string[] args)
    {
        List<Animal> animals = new List<Animal>();

        animals.Add(new Dog());
        animals.Add(new Cat());

        foreach (Animal a in animals)
        {
            Console.WriteLine(a.MakeNoise());
            a.Sleep();
        }
    }
}

public class Animal
{
    public virtual string MakeNoise() { return String.Empty; }
    public void Sleep()
    {
        Console.Writeline(this.GetType().ToString() + " is sleeping.");
    }
}

public class Dog : Animal
{
    public override string MakeNoise()
    {
        return "Woof!";
    }
}

public class Cat : Animal
{
    public override string MakeNoise()
    {
        return "Meow!";
    }
}

Obviously, the output is (Slightly paraphrased):

  • Woof
  • Dog is Sleeping
  • Meow
  • Cat is Sleeping

Since C# is often mocked for its verbose type syntax, how do you handle polymorphism/virtual methods in a duck typed language such as Ruby?

1

8 Answers 8

22

All the answers so far look pretty good to me. I thought I'd just mention that the whole inheritance thing is not entirely necessary. Excluding the "sleep" behaviour for a moment, we can achieve the whole desired outcome using duck-typing and omitting the need to create an Animal base class at all. Googling for "duck-typing" should yield any number of explanations, so for here let's just say "if it walks like a duck and quacks like a duck..."

The "sleep" behaviour could be provided by using a mixin module, like Array, Hash and other Ruby built-in classes inclue Enumerable. I'm not suggesting it's necessarily better, just a different and perhaps more idiomatically Ruby way of doing it.

module Animal
  def sleep
    puts self.class.name + " sleeps"
  end
end

class Dog
  include Animal
  def make_noise
    puts "Woof"
  end
end

class Cat
  include Animal
  def make_noise
    puts "Meow"
  end
end

You know the rest...

2
  • 2
    +1 for mixing-in the common functionality instead of inheriting it. While both methods work, this is more of "the Ruby way of doing it" (for this particular example).
    – bta
    Jun 25, 2010 at 21:58
  • It's funny that in CRuby, this is almost equivalent to inheritance: including the module animal sets the superclass of Dog to be animal, and the previous superclass of Dog to be the superclass of Animal. While this works, and may be "the ruby way of doing it", one should note that this is much less efficient than inheriting: CRuby makes a copy of the module for each class including it (for the reason stated above). Aug 10, 2016 at 17:50
15

edit: added more code for your updated question

disclaimer: I haven't used Ruby in a year or so, and don't have it installed on this machine, so the syntax might be entirely wrong. But the concepts are correct.


The exact same way, with classes and overridden methods:

class Animal
    def MakeNoise
        return ""
    end
    def Sleep
        print self.class.name + " is sleeping.\n"
    end
end

class Dog < Animal
    def MakeNoise
        return "Woof!"
    end
end

class Cat < Animal
    def MakeNoise
        return "Meow!"
    end
end

animals = [Dog.new, Cat.new]
animals.each {|a|
    print a.MakeNoise + "\n"
    a.Sleep
}
4
  • Sorry, but what version of Ruby is "class Cat : Animal"? Isn't it "<" for inheritance? Sep 26, 2008 at 4:04
  • 4
    Brent: that would be "Ruby as remembered by a Python user" Sep 26, 2008 at 4:06
  • 1
    So, will class.name output Animal or Dog? I'm genuinely curious.
    – FlySwat
    Sep 26, 2008 at 4:07
  • 1
    Yep LOL, I sometimes do "Python as remembered by a Ruby user" What's the reverse of Pythonic? Rubenesque? Sep 26, 2008 at 4:22
10

Using idiomatic Ruby

class Animal
  def sleep
    puts "#{self.class} is sleeping"
  end
end

class Dog < Animal
  def make_noise
    "Woof!"
  end
end

class Cat < Animal
  def make_noise
    "Meow!"
  end
end

[Dog, Cat].each do |clazz|
  animal = clazz.new
  puts animal.make_noise
  animal.sleep
end
3
  • I'm not a Ruby guru, but doesn't puts output the string? If so, this example calls it twice when calling sleep -- once within the method, and once with its return value (whatever that would be!) Jan 16, 2010 at 11:31
  • You're right, and puts returns nil, so the outer puts would simply print a newline, equivalent to print("#{nil}\n"), which is same as print("\n"). I didn't notice the trailing newline in my output, sorry about that.
    – manveru
    Feb 2, 2010 at 11:36
  • It should be puts animal.make_noise; animal.sleep, not puts animal.sleep Aug 10, 2016 at 17:47
2

Building on the previous answer, is this how you might do it?


Second cut after clarification:

class Animal
    def MakeNoise
        raise NotImplementedError # I don't remember the exact error class
    end
    def Sleep
        puts self.class.to_s + " is sleeping."
    end
end

class Dog < Animal
    def MakeNoise
        return "Woof!"
    end
end

class Cat < Animal
    def MakeNoise
        return "Meow!"
    end
end

animals = [Dog.new, Cat.new]
animals.each {|a|
    puts a.MakeNoise
    a.Sleep
}

(I'll leave this as is, but "self.class.name" wins over ".to_s")

6
  • 1
    MakeNoise as opposed to make_noise? Using returns? Why not follow Ruby conventions?
    – maček
    Mar 26, 2010 at 14:34
  • Why should I follow a convention I dislike intensely? IMHO, underscore is ugly and impedes the flow of reading; CamelCase is much easier to read. Did you really vote me down just for that? Meh is my answer Apr 3, 2010 at 11:39
  • 1
    @Brent- You should follow a convention (regardless of your opinion of it) if you are writing code that other people use. They will be expecting CamelCase names to refer to classes and underscore_separated names to refer to class methods and won't know anything about your thoughts on the convention. If you are only writing code for yourself to read then that's one thing but if others are going to be using your code, go with the Principle Of Least Astonishment.
    – bta
    Jun 25, 2010 at 22:01
  • @bta - I understand where you're coming from. I'm really saying two things: First, I really don't think that a deviation from the "accepted" conventions here is worth a downvote; and second, that the "accepted" conventions are ugly and force me to choose between "being in the community" and "code beauty" (in the eye of this beholder. Yes, I use returns, because I think it makes clearer code. Yes, I use CamelCase, because I think it's easier to read. And yes, I use them here because I want to show what I think is better, and let others judge. Jun 26, 2010 at 17:24
  • @Brent- I often use return as well :) I disagree with some of your opinions on style, but I agree that style here is not worth a downvote. +1 for a working solution, despite coding style.
    – bta
    Jun 28, 2010 at 15:27
1

The principle of duck typing is just that the object has to respond to the called methods. So something like that may do the trick too :

module Sleeping
  def sleep; puts "#{self} sleeps"
end

dog = "Dog"
dog.extend Sleeping
class << dog
  def make_noise; puts "Woof!" end
end

class Cat
  include Sleeping
  def to_s; "Cat" end
  def make_noise; puts "Meow!" end
end

[dog, Cat.new].each do |a|
  a.sleep
  a.make_noise
end
1

A little variant of manveru's solution which dynamic create different kind of object based in an array of Class types. Not really different, just a little more clear.

Species = [Dog, Cat]

Species.each do |specie|
  animal = specie.new   # this will create a different specie on each call of new
  print animal.MakeNoise + "\n"
  animal.Sleep
end
0

This is how I would write it:

class Animal
  def make_noise; '' end
  def sleep; puts "#{self.class.name} is sleeping." end
end

class Dog < Animal; def make_noise; 'Woof!' end end
class Cat < Animal; def make_noise; 'Meow!' end end

[Dog.new, Cat.new].each do |animal|
  puts animal.make_noise
  animal.sleep
end

It's not really different from the other solutions, but this is the style that I would prefer.

That's 12 lines vs. the 41 lines (actually, you can shave off 3 lines by using a collection initializer) from the original C# example. Not bad!

0

There's a method becomes which implements a polymorphism (by coping all instance variables from given class to new one)

class Animal
  attr_reader :name

  def initialize(name = nil)
    @name = name
  end

  def make_noise
    ''
  end

  def becomes(klass)
    became = klass.new
    became.instance_variables.each do |instance_variable|
      value = self.instance_variable_get(instance_variable)
      became.instance_variable_set(instance_variable, value)
    end

    became
  end
end

class Dog < Animal
  def make_noise
    'Woof'
  end
end

class Cat < Animal
  def make_noise
    'Meow'
  end
end

animals = [Dog.new('Spot'), Cat.new('Tom')]

animals.each do |animal|
  new_animal = animal.becomes(Cat)
  puts "#{animal.class} with name #{animal.name} becomes #{new_animal.class}"
  puts "and makes a noise: #{new_animal.make_noise}"
  puts '----'
end

and result is:

Dog with name Spot becomes Cat
and makes a noise: Meow
----
Cat with name Tom becomes Cat
and makes a noise: Meow
----
  • A polymorphism could be useful to avoid if statement (antiifcampaign.com)

  • If you use RubyOnRails becomes method is already implemented: becomes

  • Quicktip: if you mix polymorphism with STI it brings you the most efficient combo to refactor your code

I wish it helped you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.