Shim File.write in Ruby 1.8.7

I’ve had to shim File.write a couple times into scripts that might run under 1.8.7 (the version OSX shipped with prior to Mavericks).

It’s a pretty easy back-port:

1
2
3
4
5
6
7
8
9
unless File.respond_to?(:write)
class File
def self.write(filename, contents)
File.open(filename, 'w') do |file|
file.write contents
end
end
end
end

It’s better to use respond_to when determining if you need to poly-fill rather than RUBY_VERSION or something.

Maybe I should just always use the block method I wouldn’t have to keep doing this.

At least this is easier than switching all your hash literal syntax.