Any function similar to PHP's file_get_contents()?

PHP has a nice function, file_get_contents(), that lets you read a file
(local or via http) as one string.

I know in Ruby I can do…

document = open(path_to_file) { |f| f.read }

…but it’s tedious to type this again and again.

I also know I can do:

document = IO.read(path_to_file)

but this doesn’t work with URLs.

Am I missing something?

(And while we’re at it, is there any function similar to PHP’s
file_put_contents() ? )

Albert S. wrote in post #1051234:

PHP has a nice function, file_get_contents(), that lets you read a file
(local or via http) as one string.

I know in Ruby I can do…

document = open(path_to_file) { |f| f.read }

…but it’s tedious to type this again and again.

I also know I can do:

document = IO.read(path_to_file)

but this doesn’t work with URLs.

Am I missing something?

You can use open-uri from the stdlib for that:

require ‘open-uri’
document = open(‘http://www.example.com/’).read

(And while we’re at it, is there any function similar to PHP’s
file_put_contents() ? )

Not really, but usally File#open is used with a block:

File.open(“output.txt”, “w”) {|f| f.puts “Hello World!” }
=> nil
File.read “output.txt”
=> “Hello World!\n”

HTH,

  • Lars

Lars Mai wrote in post #1051239:

Albert S. wrote in post #1051234:

Am I missing something?

You can use open-uri from the stdlib for that:

require ‘open-uri’
document = open(‘http://www.example.com/’).read

But this leaves the file open…

To avoid this, you could simply write

document = open ‘http://www.example.com/’, &:read

This is actually the shortened version of

document = open ‘http://www.example.com/’ {|f| f.read}

Ruby also has an “auto close” feature:
http://www.ruby-doc.org/core-1.9.3/IO.html#method-i-autoclose-3F

Maybe this also works for URIs.

You need to require ‘open-uri’ first, then it will work. But
personally I never liked this style.

– Matma R.

2012/3/13 Jan E. [email protected]:

document = open ‘http://www.example.com/’, &:read

Should it work? isn’t “open” the same as “File.open”?:

irb> open ‘http://www.example.com/
Errno::ENOENT: No such file or directory - http://www.example.com/

2012/3/13 Bartosz Dziewoński [email protected]:

You need to require ‘open-uri’ first, then it will work. But
personally I never liked this style.

Ok, then a simpler solution:

document = open(‘http://www.google.com/’).read

:slight_smile:

From: Robert K. [email protected]

Ok, then a simpler solution:

document = open(‘http://www.google.com/’).read

document = File.read ‘http://www.google.com/

Robert:

You constantly amaze me with your cleverness. Thanks!

Wayne

On Tue, Mar 13, 2012 at 5:41 PM, Iñaki Baz C. [email protected]
wrote:

2012/3/13 Bartosz Dziewoński [email protected]:

You need to require ‘open-uri’ first, then it will work. But
personally I never liked this style.

Ok, then a simpler solution:

document = open(‘http://www.google.com/’).read

document = File.read ‘http://www.google.com/

Cheers

robert

On Tue, Mar 13, 2012 at 9:08 PM, Wayne B. [email protected]
wrote:

You constantly amaze me with your cleverness. Thanks!
Thanks, but I am afraid I wasn’t that clever this time:

$ irb19 -r open-uri
Ruby version 1.9.3
irb(main):001:0> url = ‘http://www.google.com/
=> “http://www.google.com/
irb(main):002:0> io = open url
=> #<File:/tmp/open-uri20120314-2280-ju7rp9>
irb(main):003:0> io.read.length
=> 12080
irb(main):004:0> io.close
=> nil
irb(main):005:0> io = File.open url
Errno::ENOENT: No such file or directory - http://www.google.com/
from (irb):5:in initialize' from (irb):5:inopen’
from (irb):5
from /opt/bin/irb19:12:in <main>' irb(main):006:0> File.read(url).length Errno::ENOENT: No such file or directory - http://www.google.com/ from (irb):6:inread’
from (irb):6
from /opt/bin/irb19:12:in `’

Should’ve tested this properly. :-))

Sorry for the confusion.

But we can do

irb(main):012:0> def read(x)
irb(main):013:1> io = open(x)
irb(main):014:1> begin
irb(main):015:2* io.read
irb(main):016:2> ensure
irb(main):017:2* io.close
irb(main):018:2> end
irb(main):019:1> end
=> nil
irb(main):020:0> read(url).length
=> 11117

While we’re at it we could also do

irb(main):022:0> class Object
irb(main):023:1> def tap_close
irb(main):024:2> yield self
irb(main):025:2> ensure
irb(main):026:2* close
irb(main):027:2> end
irb(main):028:1> end
=> nil
irb(main):029:0> open(url).tap_close {|io| io.read}.length
=> 11117

or

irb(main):030:0> def auto_close(x)
irb(main):031:1> yield x
irb(main):032:1> ensure
irb(main):033:1* x.close
irb(main):034:1> end
=> nil
irb(main):035:0> auto_close(open(url)) {|io| io.read}.length
=> 11105

Hmm, I think I like the variant with #read best.

Kind regards

robert

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs