Napoleon
Napoleon

Reputation: 1102

Copy file (w/o using FileUtils)

So I thought that Marshal was the best way to solve it. I load the file and immediately dump it. But I get this error: "incompatible marshal file format (can't be read) format version 4.8 required; 91.112 given"

  def self.copy_file(src, dest)
    File.open(src) do |src_file|
      File.open(dest, 'w') do |dest_file|
        Marshal.dump(Marshal.load(src_file), dest_file)
      end
    end
  end

I can't use the FileUtils because I use a certain 'Ruby variant' that doesn't have that library. And I'm not aware of any free FileUtils.dll that is standalone and redistributable. And even if it were, I still prefer my script to go without any extra .dll files. And without FileUtils File.copy() doesn't seem to exist.

Upvotes: 2

Views: 1652

Answers (1)

Jon Cairns
Jon Cairns

Reputation: 11951

How about:

def self.copy_file(src, dest)
  File.write(dest, File.read(src))
end

Or for more ancient versions of ruby that don't have File.write:

def self.copy_file(src, dest)
  File.open(dest, 'w') { |f| f.write(File.read(src)) }
end

Upvotes: 3

Related Questions