The Open-URI extends open , so you get the return type of the returned I / O stream:
open('http://www.example.com')
You must read this to get the content:
open('http://www.example.com').read[0 .. 10]
Many times, a method allows you to pass different types as a parameter. They check what it is and either use the content directly, in the case of a string, or read the handle if it is a stream.
For HTML and XML, such as RSS feeds, we usually pass the handle to the parser and let it grab the content, parse it and return an object suitable for further search:
require 'nokogiri' doc = Nokogiri::HTML(open('http://www.example.com')) doc.class #=> Nokogiri::HTML::Document doc.to_html[0 .. 10] #=> "<!DOCTYPE h" doc.at('h1').text #=> "Example Domains"
source share