Tag Archives: ruby

0x22 – Your daily class variables and constants surprise

It is not chrismas, hence no quiz, but this was strangely surprising:

class A
  @@t = "A::@@t"

  def self.s1; @@t; end
  def self.s2; T; end

class B < A
  def self.s1; @@t; end
  def self.s2; T; end

class C < A
  @@t = "C::@@t"
  def self.s1; @@t; end
  def self.s2; T; end

[ A.s1, A.s2, B.s1, B.s2, C.s1, C.s2 ]

gives you

["C::@@t", "A::T", "C::@@t", "A::T", "C::@@t", "C::T"]

0x20 – Sphinx/ThinkingSphinx vs Xapian/ActsAsXapian shootout

This benchmark compares thinking_sphinx with acts_as_xapian. We need a search engine that gives us the IDs of matching documents from a fulltext index, basic text search only.


  • one table with 200k entries with 5k of text (avg) in one column
  • one table with 500k entries with 7k of text (avg) in 6 columns
  • one table with 500k entries with 7k of text (avg) in 4 columns


Initial indexing took 10 mins with thinking_sphins and 75 mins(!!) on acts_as_xapian

Search performance

The search performance on queries that return only a few items is nearly identical.

The search performance on queries that return many items (~10000) is nearly
identical, 90% of the time is spend in ActiveRecord.

In our case – we only need IDs and not the entire documents – sphinx runs
at 0.6 secs for a particular query (with 10000 results),
where acts_as_xapian needs 4.5 secs. This is because thinking_sphinx allows
you to only fetch the ids, where acts_as_xapian insists of pulling the
models from the database. When patching acts_as_xapian to allow for pulling
ids only, we land at 0.6 vs 0.4 secs.


We will choose sphinx because

  • it is similarily fast to xapian
  • runs over the network by default
  • Indexing is way faster (I guess because acts_as_xapian pulls all data to be index from the database to hand it over, while sphinx can do that itself)
  • acts_as_xapian would need to be patched for performance reasons.

And here is some food for our beloved web spiders

0x18 – More magic symbols

With just that small addition:

class Symbol
  alias_method :old_eeq, :"===" 

  def ===(obj)
    (obj.respond_to?(self) && obj.send(self)) ||

you can do things like

case current_user
when :admin?    then "admin"
when :regular?  then "something_for_regulars"
when :guest?   then "do_guest_stuuff"
else "huh"

Isn’t that cool much more readable?

0x17 – Atomic science, revisited

I lamented earlier about the absence of a racecondition-free find_or_create implementation and somewhat promised to check back with a working implementation. YES, WE CAN: NOW is the time, folks!

As stated there the first thing missing is an advisory locking implementation. With the database as the natural choice for a synchronisation point that implementation would be DB dependant, the following being a MySQL-implementation:

module ActiveRecord::ConnectionAdapters::MysqlAdapter::AdvisoryLock
  def locked(lock)
    lock = "#{current_database}_#{lock}"

      execute "SELECT GET_LOCK(#{quote(lock)},#{TIMEOUT})"
      execute "SELECT RELEASE_LOCK(#{quote(lock)})"
class ActiveRecord::ConnectionAdapters::MysqlAdapter
  include AdvisoryLock

Now, whats still left is a find_or_create implementation. I like that one:


class ActiveRecord::Base
  def self.find_first_by_hash(hash)
    find :first, :conditions => hash

  def self.find_or_create(hash)
    find(hash) || connection.locked("#{self.table_name}.find_or_create") do
      find(hash) || create(hash)

Where is the “find_or_create_by_attribute_names”, you might ask? Well, I am not too fond of those anyways, because writing

find_by_name_and zipcode name, zipcode

adds several hundred line to the ActiveRecord implementation, doesn’t work with attributes that have an underscore in its name, and saves only a few typestrokes over

find :first, :name => name, :zipcode => zipcode

which could even be reduced down to one by an extended find() implementation, which takes a Hash as a possible first argument.


Ever done Erlang? Now I wondered how flexible ruby would be and if an erlang style pattern matcher can be done in ruby. As it turns out, I came pretty close: You might recognize this: m = matching! [ 1, … Continue reading

0x15 – public privacy protection: ruby privacy flags explained

…or: There is a difference between methodname(args) and self.methodname(args)!

The way how ruby deals with private, protected and public messages was a bit unclear to me. So I decided to dig a bit into it and to find out how it is really working. Finally I have irb installed: so lets go and explore.

This pastie shows the source code that I used for that, and here are the results.

Now, what does it mean?

  1. Public methods can (unsurprisingly) be called from anywhere
  2. Protected methods on an object can be called from that object and from other objects of the same or an derived class
  3. Private methods cannot be called via <obj>.<methodname>, not even via self.<methodname>! Consequently they can only be called from the very same object, which for example rules out private attributes.

for all of you. Before you enter the New Years Day frenzy, here are the results of last week’s puzzler. (Haven’t been a big secret anyways: every one with a Ruby interpreter could know in the meantime. Yes, that means … Continue reading