看板 Ruby 關於我們 聯絡資訊
※ 引述《larrywhy (SC)》之銘言: : 環境: : e.g. Ruby 1.9.2-p0 : 狀況: : 一直引無法引用耶...爬google爬了好久仍然沒頭緒= = '' : 看書上是說放在同資料夾就行了 還是不行= = '' : code: : [lib.rb] ↓ : --------------------------------- : def hello : print "hello I.m lib1\n" : end : --------------------------------- : [lib.rb2] ↓ : --------------------------------- : require 'lib' : #use : hello() : --------------------------------- : 用的是windows的Start Command Prompt with Ruby : 出現以下訊息 : ---------------------------------------------------------------------------- : C:\Users\acer\Desktop\My Work Station\rb>ruby lib2.rb : <internal:lib/rubygems/custom_require>:29:in `require': no such file to load 雖然已有人回答,經我的測試之後,雖然環境不太相同, 我在freebsd用1.9.1及1.8.7測試. 除了.rb的位置, 還需注意執行時的位置, 例如: /test/lib.rb /test/test.rb -> require 'lib.rb' 但在/執行ruby test/test.rb就出現 no such file to load 但在/test/ 執行 ruby test.rb 就沒有問題. 但若要確實解決這個問題,有兩個方法. 一.這在1.9及1.8都通用: $:.unshift(File.join(File.dirname(__FILE__))) require 'lib' 二.這在1.9才能用,因為1.9才有. require_relative 'lib' -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.109.16.214
larrywhy:感謝@@'' 01/08 12:15