我正在使用这个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类。
答案 0 :(得分:2)
File
类没有写方法。您必须先打开文件然后写入它。它可以这样做:
Dir.glob("*.txt").each do |file|
File.open(file, 'w') {|f| f.write replace(File.read(file))}
end