Avoiding alias_method_chain in Ruby

I currently use Ruby a lot but since I am not into developing web applications, I am not knowledgeable about Ruby on Rails. So when I watched Yehuda’s presentation about refactoring Rails I watched it not so much because of Rails but because of refactoring Ruby software.

Yehuda Katz - The Great Rails Refactor

Yehuda talks about introducing modules to be able to use super instead of alias_method_chain. I tried it out on an example and it seems to be a really useful thing to know.

Using alias_method_chain

This approach uses the popular definition of alias_method_chain

class Module
  def alias_method_chain( target, feature )
    alias_method "#{target}_without_#{feature}", target
    alias_method target, "#{target}_with_#{feature}"
  end
end

As an example we have a class Test with a method x

class Test
  def x
    'x'
  end
end

Using alias_method_chain the method x can be extended without having to introduce a new inheriting class

class Test
  def x_with_two
    "#{x_without_two}2"
  end
  alias_method_chain :x, :two
end

The method now exhibits the new behaviour

puts Test.new.x
# "x2"

Using modules

However the method x can be declared in a module which is included by Test

module X
  def x
    "x"
  end
  module_function :x
end
class Test
  include X
  public :x
end

Overloading the method now can be done by including another module which has a new definition of x. The new method x can invoke the previous one by calling super

module X2
  def x
    "#{super}2"
  end
  module_function :x
end
class Test
  include X2
end

The method now exhibits the new behaviour

puts Test.new.x
# "x2"

The issue with this approach is that overloading of the method needs to be anticipated. However at the same time the code indicates it more clearly that the first definition of x is not necessarily the final one in objects of type Test. Moreover this approach works without public definitions of methods you are not supposed to call (e.g. x_without_two).

Update: If you like to see another talk from MountainWest RubyConf 2009, you should check out Jim Weirich’s talk on The Building Blocks of Modularity.

See also:

Update: More recent post by Yehuda Katz

Update: Ruby 2.0 will introduce Module#prepend

Openmoko discontinued?

Translation of Vorläufig kein weiteres Linux-Telefon

Not another Linux-Phone

by Mathias Born. Updated 02.04.2009

Bad news for libertarians: Openmoko suspended development of the GTA03 smartphone until further notice and laid off part of its staff. The managing director said that this would be the only way for the company to survive in the long-term.

For the time being Openmoko abandons development work on the next generation of the current Freerunner smartphone. Furthermore the Taiwanese company releases almost half of its staff. This is what Openmoko managing director Sean Moss-Pultz said today at the Opensource trade show “Openexpo” in Bern. Partially the staff had been given notice; partially the staff had resigned voluntarily. “We have arrived at a critical point”, said Moss-Pultz. “This measures are absolutely necessary to even stay in business.”

Openmoko’s aim was ambitious: The subsidiary of the Taiwanese component manufacturer First International Computer (FIC) wanted to produce an Opensource-Smartphone. The source code was supposed to be freely available as well as the drivers and the specification of the components. Therefore software developers can reprogram the mobile phone at will. So far the company has produced two devices; the first one as a series of 3000 samples, the second one having been sold 10’000 times so far. Both are targeted at developers. The project was experiencing difficulties from the very beginning: The launch dates were postponed. Some batches had design faults. Furthermore there was a change in staff, which delayed development. At the middle of this year the software should have been stable enough for allowing the phone to be used in every day life.

Plan B

Openmoko now focusses on “Plan B” according to Sean Moss-Pultz. “For business reasons we have choosen the second device which we have in our pipeline.” He would not be free to disclose any more details. However it would not be about a telecommunication device. In contrast to the smartphone the first version is going to already target the mass market. Also this product is going to be developed with Opensource software he assured.

The development of the software for the Openmoko-Smartphone would continue said Moss-Pultz, however with less resources. Therefore he places great hope on the community. “Buy a Freerunner, help to fix the bugs and write new programs” he appealed to his audience during his presentation at the Openexpo. He hopes to also use those lateron on a new device. “Also in the future we want to produce mobile phones.” (Berner Zeitung)

Also see

HornetsEye demo video

This video shows current capabilities of HornetsEye. The video was created using recordMyDesktop on a Compiz-Fusion desktop. The audio track is Funki good time by Morusque.

Ruby and Machine Vision

I have made the presentation Ruby & Machine Vision available for download. The presentation gives an introduction to the Ruby programming language and its features. Furthermore it demonstrates a few capabilities of HornetsEye.

Ruby 1.9 was released

Yesterday Ruby 1.9.1 was released. Here is a small presentation about changes in Ruby 1.9. There are some new features in the syntax and the semantics of local variables was improved. For example Array.map now returns an enumerator if you don’t give a code block as argument

e1 = [ 1, 2, 3, 4 ].collect
e2 = [ 5, 6, 7, 8 ].collect
loop { puts "#{e1.next}, #{e2.next}" }

Ruby 1.9 (formerly known as YARV) also has native threads. Now it is not necessary any more to use Distributed Ruby (DRb) to make use of multicore processors. Of course this means that a Ruby extension has to be thread-safe depending on how it is being used.

There also is a lot of work on making the handling of strings aware of the current character encoding

ruby -e "puts 'Hä?'.size"
# 4
ruby1.9 -e "puts 'Hä?'.size"
# 3
ruby -e "puts 'Hä?'[1]"
# 195
ruby1.9 -e "puts 'Hä?'[1]"
# ä

Another new feature is the implementation of fibers. Fibers are like threads but they are not preemptive. This means that a fiber has to suspend itself first before execution of the calling thread can continue. Fibers allow you to implement parallel contexts with implicit locking.

Yukihiro Matsumoto's keynote at Rubyconf 2007

You can install Ruby 1.9 alongside 1.8 as follows (under GNU/Linux)

./configure --prefix=/usr/local --program-suffix=1.9
make
sudo make install

The performance of the Ruby VM was improved and the memory footprint was reduced. String operations in general will be slower though since UTF-8 now is the default character encoding. But then again Yukihiro Matsumoto’s primary goal in designing Ruby always has been to minimize the effort in programming.

Note that it will take some time until the existing Ruby-extensions get ported to 1.9.

Update: I submitted a summary of this to Slashdot and it was accepted