Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Require command not working within bash irb on Snow Leopard

I am working on Zed Shaw's Learn Ruby the Hard Way Exercise 25

http://ruby.learncodethehardway.org/ex25.html

When I navigate to the directory that holds the ruby file ex25.rb and launch IRB, I get these errors:

Larson-2:~ larson$ cd Ruby
Larson-2:Ruby larson$ ls
ex25.rb
Larson-2:Ruby larson$ irb
ruby-1.9.2-p290 :001 > require 'ex25'
LoadError: no such file to load -- ex25
    from /Users/larson/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from /Users/larson/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from (irb):1
    from /Users/larson/.rvm/rubies/ruby-1.9.2-p290/bin/irb:16:in `<main>'
ruby-1.9.2-p290 :002 > 

It seems that the require command isn't working here. Is there something I am missing?

I also tried require './25' as suggested and get these errors:

Larson-2:Ruby larson$ irb
ruby-1.9.2-p290 :001 > require './ex25'
SyntaxError: /Users/larson/Ruby/ex25.rb:1: invalid multibyte char (US-ASCII)
    from /Users/larson/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from /Users/larson/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from (irb):1
    from /Users/larson/.rvm/rubies/ruby-1.9.2-p290/bin/irb:16:in `<main>'
like image 828
Andrew Lauer Barinov Avatar asked Aug 29 '11 09:08

Andrew Lauer Barinov


2 Answers

This is because the working directory is no longer in the Ruby path in Ruby 1.9.2. This does work:

require './ex25'
like image 176
Mischa Avatar answered Oct 12 '22 16:10

Mischa


This is actually a mistake on my part when I was writing the exercise. @mischa is spot on with his solution, but you'll notice as you progress through the book that subsequent exercises use the require_relative command instead of require.

The various methods for ensuring that your working directory is in the load path are described here, but I suggest you pick one and remain consistent.

like image 28
Rob Sobers Avatar answered Oct 12 '22 18:10

Rob Sobers