About Books Credits Photos Software Rumblings Travelling Home
Monkeypatching: Ramaze 3.5 kills ActiveRecord

Stumbled on a nice monkeypatching “bug” introduced by Ramaze in Ruby 1.8.6 today. Try the following code:


p "bla"[:bogus]

what you will get is nil.

If you do:


require 'rubygems'

require 'ramaze'

p "bla"[:bogus]

you will get a TypeException: cannot convert Symbol to Integer.

I found this while trying to do an active record migration with ramaze loaded.

It will raise the exception at line 420 in active_record/connection_adapters/abstract/schema_definitions.rb.

The reason for it is a monkeypatch to struct in ramaze/snippets/struct/values_at.rb:


class Symbol

  undef_method :to_int if method_defined?(:to_int)

end

class Struct

  def values_at(*keys)

    if keys.all?{|key| key.respond_to?(:to_int) }

      keys.map{|key| values[key.to_int] }

    else

      keys.map{|k| self[k] }

    end

  end

end

A quick IRC session in #ramaze and Ara Howard came up with the following fix:


class Struct

    def values_at(*keys)

        if keys.all?{|key| key.respond_to?(:to_int) and not key.is_a?(Symbol) }

            keys.map{|key| values[key.to_int] }

        else

            keys.map{|k| self[k] }

        end

    end

end

Now, you must note that in Ruby 1.9 “bla”[:bogus] will always throw a TypeException, but the problem here is that AR (as of this writing at version 2.0.2) is still 1.8.6 compatible and this monkeypatch is a heap of trouble.

The workaround – until a Ramaze release fixes this – is to require the ramaze gem after any AR migrations.