File:Class的未定义方法`open_and_process'

时间:2013-08-19 23:14:17

标签: ruby undefined

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'

1 个答案:

答案 0 :(得分:1)

如果您想编写File's课程方法,可以打开File课程:

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