#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    1
    Rep Power
    0

    LoadError on Windows and Ubuntu when trying to run a porgram


    Hi,

    I'm new to Ruby, just trying to learn it.

    I installed Ruby via the ruby installer on my win 7 Pro 64 bit machine. 'ruby -v in command prompt shows folllowing.
    ruby 1.9.3p194 (2012-04-20) [i386-mingw32]

    In my learning, I have a small program named book.rb in my c:\scripts\ruby directory

    class Book
    end

    When I open the irb in the same directory (in command prompt) where the above script is located, and type require 'book.rb' and hit enter, I get the following error:

    LoadError: cannot load such file -- book.rb
    from C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from (irb):1
    from C:/Ruby193/bin/irb:12:in `<main>'

    I do not get this error in Machintosh (Lion). Rather, it gives me following:
    => true

    Why in windows I get the error? Does anyonme know..

    Appreciate all help!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    83
    Rep Power
    39
    The difference isn't between Windows and OSX, it's between Ruby >1.9.2 and earlier version of Ruby. Since Ruby 1.9.2 the current directory is no longer included in the load path, so you can't just require files in the current directory using their relative filename. I assume your OSX system has a version of Ruby before 1.9.2 installed.

    To fix your issue, you can either require the file when starting irb using the -r flag (i.e. invoke "irb -r book" instead of just irb) or you can require the absolute filename using require "#{Dir.pwd}/book" or you can add the current directory to the load path using $LOAD_PATH << "." .

    Note that in ruby files it is now recommended to use the new require_relative method to require files that are in the same directory as the file from which you're requiring. This avoids many problems that using require to do this had. That doesn't help you in irb though.

IMN logo majestic logo threadwatch logo seochat tools logo