Friday, November 19, 2010

PHP to Ruby: file_get_contents and file_put_contents

PHP had useful functions for writing/reading files without opening handle.

http://pastebin.com/Pw04Vgs6
There is some code implementing this feature in Ruby.
It extends Ruby’s basic String object with handy ‘get’ and ‘put’ methods.
Example usages and analogues in PHP are provided.

Note: Reading file may not be as efficient as PHP’s file_get_contents because PHP tends to use memory mapping techniques (if supported by OS).
Consider reading short thread about Ruby File.read.

Code under the cut: (same as in pastebin)