2012-09-04 38 views
1

在我的主應用程序文件(tinyclone.rb)的頂部,Sinatra應用程序中,此散列用於需要幾個寶石和一個文件的髒語。 RB「。髒字文件與tinyclone.rb文件位於同一目錄中。Ruby:與主應用程序文件不在IRB中加載的文件位於同一目錄中

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

當我加載IRB

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

的tinyclone.rb文件加載所有這些文件/寶石中的散列,除了dirty_words文件

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>' 

人解釋爲什麼這一個文件不被正確使用?

回答

1

Ruby 1.9不包含當前目錄到搜索路徑中。

有幾個選項來解決此問題:

# 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__) 
相關問題