2012-05-09 69 views
0

我這個工作的Ruby代碼,我想在windowspc上用exerb做一個可執行文件。 當我編譯hello_world.rb是沒有問題的運行可執行文件,但與此代碼 Exerb創建我的可執行文件,但是當我運行它,我得到以下錯誤Ruby Exerb:丟失文件類

undefined method `write' for File:Class (NoMethodError) 

下面的代碼

def replace text 
    replacements = [ 
    {:pattern => /(^ARFD0001\|.*)(FAC_12125)/, :replace_with => '\1FAC_12102'}, 
    {:pattern => /^ARFD0001\|121\|25\|ZIEFAC\|/, :replace_with => 'ARFD0001|121|02|ZIEFAC|'}, 
    {:pattern => /(^ARFD0010\|.*)(12125203)(\d{3})/, :replace_with => '\112102181\3'}, 
    {:pattern => /(^ARFD0010\|.*)(2030341401)/, :replace_with => '\1181701500'}, 
    {:pattern => /(^ARFD0019\|.*)(12125203)(\d{3})/, :replace_with => '\112102181\3'}, 
    {:pattern => /(^ARFD0019\|\d*\|\d*\|\d*)(\|{2})/, :replace_with => '\1|PRINT|'}, 
    {:pattern => /^ARFD0009\|121\|25\|/, :replace_with => 'ARFD0009|121|02|'} 
    ].each{|replacement|text.gsub!(replacement[:pattern], replacement[:replace_with])} 
    text 
end 

Dir.glob("*.txt").each{|file|File.write(file, replace(File.read(file)))} 
#line above gives the error in Exerb 

如何爲了得到這個?代碼沒有什麼問題,在Ruby解釋器中可以工作,但似乎我必須告訴Execrb包含File類。

回答

2

File類沒有寫​​入方法。您必須先打開文件,然後才能寫入文件。它可以這樣做:

Dir.glob("*.txt").each do |file| 
    File.open(file, 'w') {|f| f.write replace(File.read(file))} 
end 
+0

如果我檢查File類的確看不到寫入方法。但是,那麼代碼如何在普通的Ruby腳本中起作用呢?在家裏試過也沒有問題,文件寫入。我明天在工作中編譯你的代碼,看看它有什麼不同。 – peter

+1

你可能正在處理不同的ruby版本,ruby 1.8.7沒有它,但紅寶石1.9.3 – fsainz