March, 2010

Rails::Generator destroys Namespaces

Sunday, March 28th, 2010

The cool thing is that Rails’ script/generator script can be invoked via an API call

Rails::Generator::Scripts::Generate.new.run(%w(controller blog), :destination => '/tmp')

so you can create assets in your code, or test the generator.

Where is my JavascriptGenerator?

The bad thing is, that after you used Rails::Generator, you can’t rely on autoloading anymore, at least for classes ending with Generator.

When trying to use my own class JavascriptGenerator (which has nothing to do with Rails at all) I got

JavascriptGenerator::Base.new # NO rails...

Rails::Generator::GeneratorError: Couldn't find 'javascript' generator

which is really annoying. That’s Rails “magic” again (somewhere in rails/generator/…):

class Object
  class << self
    def lookup_missing_generator(class_id)
      if md = /(.+)Generator$/.match(class_id.to_s)
        name = md.captures.first.demodulize.underscore
        Rails::Generator::Base.lookup(name).klass
      else
        const_missing_before_generators(class_id)
      end
    end
 
    unless respond_to?(:const_missing_before_generators)
      alias_method :const_missing_before_generators, :const_missing
      alias_method :const_missing, :lookup_missing_generator
    end
  end
end

That’s a typical example how Rails assumes things automatically that you don’t expect.

I personally think that extending Object should be allowed to non-magicians only.

Running Ruby 1.9.1 with RVM on Ubuntu karmic

Sunday, March 21st, 2010

It’s finally time to try out things with Ruby 1.9 - people start complaining that my libs don’t run with 1.9. Sorry for that. I’m still sticking to “Never touch a running system!”, so why should I use Ruby 1.9 when 1.8 works great?

RVM

First I discovered rvm which is a great tool for hosting different Ruby versions in your shell and switching environments on the fly.

That’s

> sudo gem install rvm

Now go and install Ruby 1.9.1 (couldn’t get 1.9.2 working)

> rvm install 1.9.1

This will install Ruby 1.9.1 in your home directory, which is awesomely cool.

Switching to the new ruby/gems is nothing more than

> rvm use 1.9.1

Running Rails

Things start getting shitty now. When trying to run Rails with ruby 1.9 I got something

no such file to load -- openssl

rvm explains to install the package openssl. That doesn’t work for me

$ rvm package install openssl
Package 'openssl' is unknown.
Usage: 'rvm package {install,uninstall} {openssl,zlib,readline,iconv,ncurses}'

which is… strange.


Note: Wayne fixed that in the rvm release 0.1.24 - so the rest of this article is useless. Thanks, cowboy!


Following the great directions here didn’t help either, although it’s a good post.

At some point make failed with

error: ruby/io.h: No such file or directory

The solution

I finally ended up fixing it with

$ sudo apt-get install libopenssl-ruby1.9.1
$ cp /usr/lib/ruby/1.9.1/i486-linux/openssl.so ~/.rvm/rubies/ruby-1.9.1-p378/lib/ruby/1.9.1/i686-linux/
$ cp -R /usr/lib/ruby/1.9.1/openssl* ~/.rvm/rubies/ruby-1.9.1-p378/lib/ruby/1.9.1/

and the shit’s working.

Installing gems

rvm provides some decent way to install gems, it works like

$ rvm 1.9.2 gem install cells --no-ri --no-rdoc

When running ruby 1.8.7 be sure to install sqlite3-ruby, not sqlite3. The latter version is only for 1.9 and will throw arbitrary

NameError: uninitialized constant Encoding

Hope that helps!