Вопрос

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>'
Это было полезно?

Решение

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

require './ex25'

Другие советы

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top