7

Trying to go through the tekpub rack tutorial but run into this error.

Boot Error

Something went wrong while loading app.ru

LoadError: cannot load such file -- haiku

There is a file named haiku.rb in the same directory as the app I am trying to run but I get the above error while trying to run the program. Here is the code:

class EnvironmentOutput


  def initialize(app=nil)
    @app = app
  end


  def call(env)
    out = ""

    unless(@app.nil?)
 response = @app.call(env)[2]
 out+=response
end

env.keys.each {|key| out+="<li>#{key}=#{env[key]}</li>"}
["200",{"Content-Type" => "text/html"},[out]]
  end
end

require 'haml'
require 'haiku'

class MyApp
  def call(env)

  poem = Haiku.new.random
  template = File.open("views/index.haml").read
  engine = Haml::Engine.new(template)
  out = engine.render(Object.new, :poem => poem)

   ["200",{"Content-Type" => "text/html"}, out]
  end
end

use EnvironmentOutput
run MyApp.new

I'm sure its a small error as the code is the same as in the tutorial and it works for him...

Thanks

AFraser
  • 996
  • 4
  • 13
  • 27
  • 5
    If you're running on ruby 1.9, you might want to try `require './haiku.rb'`, or appending the current directory to your load path (`$:.append(File.dirname(__FILE__))`) and then doing `require 'haiku'. – Frost Oct 09 '11 at 11:04
  • Thanks that fixes it. Where would I look to try and find that informtaion. The Rack Documentation or Ruby Docs? – AFraser Oct 09 '11 at 21:17
  • Reworded my comment as a proper answer just now. It's a bit more descriptive. – Frost Oct 10 '11 at 08:09

1 Answers1

21

You'll want to read up on ruby load path (either $LOAD_PATH or $:). By default, ruby has a load path which includes wherever your gems are installed, which is why you can do require 'haml' without providing the full path to where your haml gem is located.

When you type require 'haiku', you're basically telling ruby to look for some file called haiku.rb somewhere in it's load path, and the LoadError comes from ruby not finding your haiku.rb file in any of the directories listed in $LOAD_PATH (or $:, which is just shorthand for $LOAD_PATH).

You can solve this in one of (at least) two ways:

  1. change require 'haiku' to require File.dirname(__FILE__) + '/haiku.rb' to explicitly tell ruby what file to load

  2. add the current working directory to your load path: $:.push(File.dirname(__FILE__)). This way you can keep the require 'haiku' part.

Frost
  • 11,121
  • 3
  • 37
  • 44
  • 3
    You might also look into `require_relative` if you're running ruby 1.9. – Frost Aug 29 '12 at 17:08
  • I don't think `.append` is a method on the $LOAD_PATH array. Should the code snippet in #2 be `$:.push` or have I got something wrong? – Ryan Nov 24 '12 at 01:41
  • You are entirely correct. It should either be `$:.push` or `$: <<`. I'll update my answer. – Frost Dec 04 '12 at 13:12
  • As a side note on the `$:.push` part, this will append the current directory to the end of the load path. In order to prepend it to the load path, use `unshift` instead of `push` – Frost Apr 16 '13 at 13:13