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"

module M2
  def foo
    puts "M2"

and then include them in a class in a specific order

class C
  include M1
  include M2

method from the last included module will be used. # => "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"

class C
  def foo
    puts "C"
  include M

should print “M”. But it doesn’t. # => "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.

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"

and include it in “C”.

class C
  include M

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" then "M"

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

Solution to Code Kata Fifteen

5 11 2007

Just felt like doing some programming exercise. My bookmarks led me to the code kata 15. First, the problem:

Think of binary numbers: sequences of 0’s and 1’s. How many n-digit binary numbers are there that don’t have two adjacent 1 bits? For example, for three-digit numbers, five of the possible eight combinations meet the criteria: 000, 001, 010, 011, 100, 101, 110, 111. What is the number for sequences of length 4, 5, 10, n?

Having worked out the pattern, there’s a second part to the question: can you prove why that relationship exists?

Now, the solution.

Let’s call the function that calculate the number of n-digit binary numbers without two adjacent 1 bits a(n).

Now let’s define two helper functions. a0(n) returns number of those binary numbers that end with zero, and a1(n) returns number of those ending with one. Thus, it’s obvious that:

a(n) = a0(n) + a1(n)

Now, suppose we already have a set of (n-1)-bit numbers generated and now, based on that, we want to generate a new set of n-bit numbers. We’ll do that by adding a single bit to the end of each (n-1)-bit number. Because we care only about adjacent 1s, we can add 0s to the end of every (n-1)-bit number. Thus:

a0(n) = a(n-1)

On the other hand we can’t add 1s to each (n-1)-bit number. We can only add 1 to numbers which had 0 at the end. Thus:

a1(n) = a0(n-1)

By simple substitution we can rewrite the last equation into the following one:

a1(n) = a(n-2)

Get back to the definition of a0(n) and a1(n) and make the final substitution:

a(n) = a(n-1) + a(n-2)

Doesn’t it look familiar? Now you only have to say that a(1) = 2 and a(2) = 3 and you’re done.

spec plugin compatible with nose 0.10

12 10 2007

If you use the spec plugin (part of the pinocchio package) and want to upgrade to the new version of nose, you can safely do this now, because here is the new spec plugin, (I hope) fully compatible with the new plugin API. Note that it’s not an official release of pinocchio, but just a testing version for the impatient. I haven’t changed other pinoccho plugins, so you’re safe to upgrade your pinocchio 0.1 installation to this version. Let me know if something doesn’t work as expected.

PVS update

29 07 2007

It’s been quiet here lately, but only because I’m busy with PVS – my SoC project. ;-) PVS stands for Patch Verification System and I’m building it to support patch review process for CPython. There’s not much time left before the end of SoC 2007, so I won’t tell you the whole story now. Instead, I will share with you few insights about development process and tools I’ve used. OK, here we go.

Trac logoSometimes Trac is an overkill. Text files in reST format are often more than enough. You also get a bonus by keeping all your project-related stuff (tests, documentation, todos) in one place (i.e. not scattered among repository, wiki and bugs manager).

Django logoI’m using two ORMs inside PVS: Elixir+SQLAlchemy and Django ORM. Both work great and saved me a lot of tedious work. I like the way SQLAlchemy handles references to other tables, although its select syntax is slightly weird (although surely powerful). Django ORM may not be as functional as SQLAlchemy, but its main advantage is its good integration with the rest of the framework. Tweaking the admin views is both pleasant and addictive (something like customizing desktop settings of your new Ubuntu install ;-). You’ve been warned.

Did I mention that both ORMs work on the same database without any problems at all? A big thanks to both libraries’ authors for not using magic tables/fields or strange implicit conventions.

Ohloh Metric for PythonDuplicating the whole source checkout tree of a project of CPython size (about 100Mb in almost 12.000 files) is quite expensive, as I soon realized (better sooner than later). Hopefully I don’t have to do this – patch has –dry-run option and subversion client supports revert operation. This combination has truly saved me. :-)

Twill rocks. I written a patch reporter for Roundup in less than half an hour. Thanks Titus!

@Python decorators are not used often, but applied carefully can be very powerful. In PVS I defined daemonized decorator which makes decorated function execute in the background as a separate process. It also creates a PID file in a configured directory (like /var/run/pvs/), so you can easily manage the daemon (e.g. send signals to it).

Bazaar logoAfter initially using Subversion I recently switched to Bazaar, mostly for its ability to work offline. It’s been working fine so far. Because I’m working alone I didn’t have occasion to use its merging capabilities, so can’t really comment on those. When it comes to speed, it’s visibly faster than Subversion, mostly because it doesn’t have to process anything over the wire. Making a backup and sharing is easier – just archive the branch directory and you’re done. svn2bzr worked without problems, so if you’re a Subversion user – give Bazaar a try.

There are functions which I use over and over again in different Python applications that I write. Simple ones like read_file_contents/write_file_contents and more complicated beasts like daemonize or run_command. Those get copy&pasted between code repositories, which isn’t really DRY. Yeah, I could clean them up and put mk-utils package on PyPI, but I’m too lazy and this is a stupid idea anyway. A central repository of those functions and a simple way to cherry-pick – now that would make my day. Hey, why do we need whole libraries anyway? ;-)

And finally: grab the code. :-)

Two quotes

16 06 2007

Joel Spolsky:

People should take responsibility and ownership of the things that they specify. If something’s wrong with the spec, there should be a designated spec owner, with their name printed right there on the spec, who is responsible for fixing it.

Venkat Subramaniam and Andy Hunt:

Blame doesn’t fix bugs. Instead of pointing fingers, point to possible solutions. It’s the positive outcome that counts.

PVS architecture draft

28 05 2007

If you’re interested in my Python patch verification system project (or want to get interested :-), check out notes on system architecture I just added. Feel free to comment here or on the wiki, I’d love to hear your feedback.

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:
>>[1,2,3,4]).map { |x| x**2 }.class
=> Array

At least Haskell does the right thing:
Main> typeOf( (\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.