0

我这个工作的 Ruby 代码我想在 windowspc 上用 exerb 制作一个可执行文件。当我编译 hello_world.rb 时,运行可执行文件没有问题,但是使用此代码 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 类。

4

1 回答 1

2

类上没有 write 方法File。您必须先打开文件,然后才能对其进行写入。可以这样做:

Dir.glob("*.txt").each do |file|
  File.open(file, 'w') {|f| f.write replace(File.read(file))}
end
于 2012-05-09T16:54:56.217 回答