A. Romeu
A. Romeu

Reputation: 302

One-liner copy file in ruby

Is there a one-line way to copy a file to another in Ruby, ensuring the files are closed after the operation finishes?

The best I can come up with is this but I'm not sure the files are closed

open(ARGV[1], 'w').write(open(ARGV[0]).read)

Upvotes: 0

Views: 106

Answers (1)

sawa
sawa

Reputation: 168101

File.write(ARGV[1], File.read(ARGV[0]))

is a bit shorter and will ensure the files will be closed.

Upvotes: 3

Related Questions