Require command not working within bash irb on Snow Leopard
Asked Answered
G

2

8

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>'
Goran answered 29/8, 2011 at 9:24 Comment(2)
Well, that's a different error message, so a different question. Does this help? #1740336Copulation
FYI the latter errors were caused because the file was encoded as UTF-16. Once it got changed to UTF-8, they went away.Goran
C
14

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

require './ex25'
Copulation answered 29/8, 2011 at 9:35 Comment(5)
Because of this : #4966056Supererogate
ruby 1.9.2 doesn't include the current directory in the loadpath. 1.8.7 did.Definiendum
Yes, in 1.9 the current directory is not in the load path. You could also do $: << '.' before the first require for adding it.Cost
Thanks, Benoit! Updated my answer with that link. Also thanks pferdefleish and undur_gongor.Copulation
@undur_gongor: you can also use the "I" option: irb -I . -r ex25Basilbasilar
B
2

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.

Bibliophage answered 31/8, 2011 at 19:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.