Ruby: initialize and super

Tuesday, July 13, 2010

One of the reasons to like Ruby if you come from a C#/C++/Java background is how certain constructs will tend to "just make sense" even though Ruby will throw in a twist here and there.

Consider the case of inheritance and initialize methods.

class Employee < Person
end

emp = Employee.new

Let's say you run the code and get an error on the line of code creating the Employee object : "wrong number of arguments". Seems odd, because Employee doesn't look like it needs an argument for creation, but then you poke around and find the definition for Person (the base class).

class Person    
    def initialize(name)
        @name = name
    end    
end

Ah, so we need a name to build a person object. The C# developer might instantly think of this scenario:

class Person
{
    public Person(string name)
    {
        this.name = name;
    }

    string name;
}

class Employee : Person
{
}

 

With C# you have to give Employee a constructor that will forward a name parameter to the base constructor.

class Employee : Person
{
    public Employee(string name) : base(name)
    {            
    }
}

In Ruby you can take this approach, too. It's a good approach if you need to perform some extra initialization for employees, like require a salary figure.

class Employee < Person
    def initialize(name, salary)
        super(name)
        @salary = salary
    end
end

emp = Employee.new("Chuck", 100)

It's Ruby's super keyword that acts like the base keyword in C#. The twist here is how Ruby will just make things work in the trivial case.

class Employee < Person
end

emp = Employee.new("Chuck")

In this code, "Chuck" will arrive at the initialize method of Person without any explicit code from us.

You might call it magic, or you might call it wonderful.


Comments
gravatar Doug Rathbone Wednesday, July 14, 2010
I love ruby and am getting more into it by the day but in this case (like a few others in ruby) it scares me. Unlike you it doesn't "make sense" it's basically an implicit initializer. I'm not too much of a fan of implicit things in code as when your not a ninja of the language ( I am not when it cones to ruby) it makes it harder to trouble shoot etc as you don't know when to assume that something will work without prior knowledge. It's probably just me, but it's just how I feel...
gravatar Alexandre Gazola Wednesday, July 14, 2010
Nice post!

Some Ruby's shortcuts really come in handy.
gravatar scott Wednesday, July 14, 2010
@Alexandre - thanks!

@Doug - I can appreciate the feeling. Language choice is often very personal, I think.
gravatar Rob Thursday, July 15, 2010
I finally sat down and started playing with RoR last night - and REALLY enjoyed myself..

Little "quirks" like this are kind of what scares a lot of us "compiled-heads" but, I think things like this are a relatively small price to pay for a lot of the nice things Ruby brings to the table..

Thanks for the post!

Besides, if you are working in this often, it would be second nature..
Patrik H&#228;gne Friday, July 16, 2010
This is not magic. This is not wonderful. This is just how it _should_ work in any language. Just to clarify, I'm a C# dev, not one of those "everylanguageshouldbedynamicordie-kind of guys".
gravatar gangelo Thursday, September 9, 2010
ruby rocks. been a c++/c# guy long time. ruby is my preferred language. love it.
Phil Thompson Saturday, October 16, 2010
I'm not sure it _should_ work in any language. In other languages it wouldn't make sense and you'd quickly end up with a mess. This it scares people like Doug and others because they see it in the context of the languages they are used to.

Ruby is so expressive that you realise (gradually) that it _does_ make sense in _this_ language.

Dynamically typed languages are very different beasts. It's easy to make a mess that's why unit testing, TDD and BDD are even more important.

gravatar Monday, November 26, 2012
i love c#, i love ruby more, i love rails much more!
Comments are now closed.
by K. Scott Allen K.Scott Allen
My Pluralsight Courses
The Podcast!