Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 未定义的方法“开放式”和“进程';文件:类_Ruby_Undefined - Fatal编程技术网

Ruby 未定义的方法“开放式”和“进程';文件:类

Ruby 未定义的方法“开放式”和“进程';文件:类,ruby,undefined,Ruby,Undefined,下面是我的文件_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(NoMethod

下面是我的文件_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)

未定义的方法“打开”和“处理”如果要编写
文件的
类方法,可以打开
文件
类:

class File
  def self.open_and_process(*args)
    f = File.open(*args)
    yield f
    f.close
  end
end

def File
不是打开File类的方式,而是
class File