文件的移动

将文件从一个地方移到另一个地方,我们一般会用到FileUtils,这个很简单,下面看一个示例:

 

FileUtils.move("/path/to/file", "/path/to/new/file")

 

那我们能不能像下面这样去实现同样的功能呢?

 

"/path/to/file".move("/path/to/new/file")

 

可以,写个方法就ok了:

 

class String
  def move(to = nil)
    if to
      if File.exist?(self)
        FileUtils.move(self, to)
      end
    end
  end
end

 

你可能感兴趣的:(文件)