Convert PHP code into Ruby!
RSS icon Email icon Home icon
  • file_get_contents

    Posted on November 18th, 2010 RubyLove 1 comment

    The file_get_contents() function in PHP reads the content of a file into a string (or reads the HTML of a web page into a string).


    $body = file_get_contents( '' );
    echo $body;
    ... HTML source of ...

    To replicate this functionality in Ruby, we will use the Net/HTTP class from Ruby.


    require 'net/http';
    uri = '';
    body = Net::HTTP.get_response(URI.parse(self)).body;
    p result
    # ... HTML source of ...

    This page was contributed by Cemil Necefov. Thanks!


    One response to “file_get_contents”

    1. require ‘open-uri’
      puts open(’’).read

    Leave a reply