Leahcim
Leahcim

Reputation: 42069

Ruby: File in same directory as main application file not being loaded in IRB

At the top of my main application file (tinyclone.rb) in a Sinatra app, this hash is used to require several gems and one file 'dirtywords.rb.' The dirty words file is in the same directory as the tinyclone.rb file.

%w(rubygems data_mapper sinatra haml dm-core dm-timestamps dm-types uri restclient xmlsimple dirty_words).each  { |lib| require lib}

When I load the tinyclone.rb file in irb

require '/Users/mm/sites/cloning/tinyclone.rb'

it loads all of those files/gems in the hash, except for the dirty_words file

cannot load such file -- dirty_words
    from /Users/mm/.rvm/rubies/ruby-1.9.3-rc1/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:55:in `require'
    from /Users/mm/.rvm/rubies/ruby-1.9.3-rc1/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:55:in `require'
    from /Users/mm/sites/cloning/tinyclone.rb:1:in `block in <top (required)>'
    from /Users/mm/sites/cloning/tinyclone.rb:1:in `each'
    from /Users/mm/sites/cloning/tinyclone.rb:1:in `<top (required)>'
    from /Users/mm/.rvm/rubies/ruby-1.9.3-rc1/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from /Users/mm/.rvm/rubies/ruby-1.9.3-rc1/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from (irb):1
    from /Users/mm/.rvm/rubies/ruby-1.9.3-rc1/bin/irb:16:in `<main>'

Can anyone explain why that one file is not being required properly?

Upvotes: 1

Views: 240

Answers (1)

Serge Balyuk
Serge Balyuk

Reputation: 3462

Ruby 1.9 doesn't include current dir into search path.

There are several options to work around this:

# Ruby 1.9 only:
require_relative 'dirty_words'

# Ruby 1.9 and 1.8 compatible:
require File.expand_path('dirty_words', File.dirname(__FILE__))

# Both options above imply that you separate `dirty_words` from the list of libs which
# are present in $LOAD_PATH, and require it separately. Here's another option which
# should allow you to keep require iteration as it is, though it's considered dirty:
$LOAD_PATH << File.dirname(__FILE__)

Upvotes: 1

Related Questions