How to require Rake >= 0.9 with Ruby >= 1.9

Very simple problem, judging by this title.

  • I have Ruby 1.9.2
  • I installed Rake 0.9.2.2 with RubyGems (gem install rake)
  • I want to use Rake 0.9.2.2 in my Ruby environment (Rake 0.9.2.2 defines Rake::DSL module). How can I do ?

Simple answer (the one everyone is expecting to work):

require 'rake' # => true
Rake::DSL      # => NameError: uninitialized constant Rake::DSL

=> Wrong

The problem here is that Ruby 1.9 comes already with Rake 0.8.7 in its standard library, and require will always give priority to standard library over installed gems. So whatever Rake version you may install on your system, require 'rake' will always come up with version 0.8.7.

So basically, 2 solutions:

  1. Force Rake version using RubyGems:
    gem 'rake', '0.9.2.2' # => true
    require 'rake'        # => true
    Rake::DSL             # => Rake::DSL
    
  2. Include a rb file present in Rake 0.9.2.2 and unknown to 0.8.7, before requiring rake:
    require 'rake/dsl_definition' # => true
    require 'rake'                # => true
    Rake::DSL                     # => Rake::DSL
    

Hope this helps !

About Muriel Salvan

I am a freelance project manager and polyglot developer, expert in Ruby and Rails. I created X-Aeon Solutions and rivierarb Ruby meetups. I also give trainings and conferences on technical topics. My core development principles: Plugins-oriented architectures, simple components, Open Source power, clever automation, constant technology watch, quality and optimized code. My experience includes big and small companies. I embrace agile methodologies and test driven development, without giving up on planning and risks containment methods as well. I love Open Source and became a big advocate.
Howto, Rake, Ruby , , , , , , , , ,

Leave a Reply

Your email address will not be published.