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)
# Code by Cemil Necefov (Jamil Najafov) # Implementing basic file_get_contents [http, local file] and file_put_contents[local file: [write, append]] # Extending String class String def get case self.scan(/^[a-zA-Z]+(?=\:\/\/)/) # get wrapper name when 'http' return Net::HTTP.get_response(URI.parse(self)).body else return File.read(self) end end def put (tx, mode='w') File.open( self, mode ) { |file| file.write tx } end end # Usage # PHP analogue 'localfile.txt'.put 'some text data' # file_put_contents('localfile.txt', 'some text data') 'localfile.txt'.put 'another bit', 'a' # file_put_contents('localfile.txt', 'another bit', FILE_APPEND) print 'localfile.txt'.get # echo file_get_contents('localfile.txt') print 'http://www.google.com'.get # echo file_get_contents('http://www.google.com')
No comments:
Post a Comment