Including a module in Ruby

18 11 2007

If we define two modules, with methods that have the same names

module M1
  def foo
    puts "M1"
  end
end

module M2
  def foo
    puts "M2"
  end
end

and then include them in a class in a specific order

class C
  include M1
  include M2
end

method from the last included module will be used.

C.new.foo # => "M2"

So it looks like the methods are “copied” into the including class, so that the last definition of “foo” gets precedence. That’s how I thought about including Ruby modules initially.

But if that was the case the following example

module M
  def foo
    puts "M"
  end
end

class C
  def foo
    puts "C"
  end
  include M
end

should print “M”. But it doesn’t.

C.new.foo # => "C"

It calls the class “C” version of the “foo”, so the method can’t be redefined during the include.

What actually happens (and what I learned from “Include” part of Chapter 4 of Ruby Hacking Guide) is that the included module gets injected into the class hierarchy right above “C”.

class C
  def foo
    puts "C"
    super # Calling to see what the superclass defined.
  end
end

Let’s check what the hierarchy looks like before the inclusion of M.

C.ancestors # => [C, Object, Kernel]

Now let’s define “M”

module M
  def foo
    puts "M"
  end
end

and include it in “C”.

class C
  include M
end

Let’s check how it affected the class hierarchy.

C.ancestors # => [C, M, Object, Kernel]

Module “M” got injected as a direct superclass of “C”.

C.new.foo # => "C" then "M"

As C#foo calls super it’s now obvious how we got that output.

Advertisements




Minor annoyance

19 05 2007

Map operation on sets isn’t closed, both in Python:

>>> type( map(lambda x: x**2, set([1,2,3,4])) )
<type 'list'>

and Ruby:
>> Set.new([1,2,3,4]).map { |x| x**2 }.class
=> Array

At least Haskell does the right thing:
Main> typeOf( Set.map (\x -> x^2) (Set.fromList([1,2,3,4::Integer])) )
Set Integer

An obvious workaround is to operate on lists and convert to sets once you need them. You have to ask yourself whether it will cause performance problems in your application.





The Whitespace Thing

6 08 2006

Don’t even dare to mention now that Python is the only one that have the whitespace quirks

#!/usr/bin/ruby

def a(x=4)
    x+2
end

b = 1

puts a + b # => 7
puts a+b # => 7
puts a+ b # => 7
puts a +b # => 3 !!!

puts b + a # => 7
puts b+a # => 7
puts b+ a # => 7
puts b +a # => 7