Bellow是我的file_open.rb
def File
def self.open_and_process(*args)
f = File.open(*args)
yield f
f.close
end
end
File.open_and_process("testfile", "r") do |file|
while line = file.gets
puts line
end
end
我一直收到的错误是: File:Class(NoMethodError)的未定义方法`open_and_process'
答案 0 :(得分:1)
如果您想编写File's
课程方法,可以打开File
课程:
class File
def self.open_and_process(*args)
f = File.open(*args)
yield f
f.close
end
end