This page contains automated test results for code from O'Reilly's Ruby Cookbook. If this code looks interesting or useful, you might want to buy the whole book.

Parsing URLs
CodeExpectedActual
require 'uri'
URI.parse('https://www.example.com').scheme
"https" "https"
URI.parse('http://www.example.com/').host
"www.example.com" "www.example.com"
URI.parse('http://www.example.com:6060/').port
6060 6060
URI.parse('http://example.com/a/file.html').path
"/a/file.html" "/a/file.html"
URI.split('http://example.com/a/file.html')
["http", nil, "example.com", nil, nil, "/a/file.html", nil, nil, nil] ["http", nil, "example.com", nil, nil, "/a/file.html", nil, nil, nil]
URI::HTTP.component
[:scheme, :userinfo, :host, :port, :path, :query, :fragment] [:scheme, :userinfo, :host, :port, :path, :query, :fragment]
URI::MailTo.component
[:scheme, :to, :headers] [:scheme, :to, :headers]
URI::HTTP.build(:host => 'example.com', :path => '/a/file.html',
                :fragment => 'section_3').to_s
"http://example.com/a/file.html#section_3" "http://example.com/a/file.html#section_3"
class URI::Generic
  def dump
    component.each do |m|
      puts "#{m}: #{send(m).inspect}"
    end
  end
end
url = 'http://leonardr:pw@www.subdomain.example.com:6060' +
	'/cgi-bin/mycgi.cgi?key1=val1#anchor'
URI.parse(url).dump
scheme: "http"
userinfo: "leonardr:pw"
host: "www.subdomain.example.com"
port: 6060
path: "/cgi-bin/mycgi.cgi"
query: "key1=val1"
fragment: "anchor"
scheme: "http"
userinfo: "leonardr:pw"
host: "www.subdomain.example.com"
port: 6060
path: "/cgi-bin/mycgi.cgi"
query: "key1=val1"
fragment: "anchor"
URI::parse('ftp://leonardr:password@ftp.example.com/a/file.txt').dump
scheme: "ftp"
userinfo: "leonardr:password"
host: "ftp.example.com"
port: 21
path: "/a/file.txt"
typecode: nil
scheme: "ftp"
userinfo: "leonardr:password"
host: "ftp.example.com"
port: 21
path: "/a/file.txt"
typecode: nil
uri = URI::parse('mailto:leonardr@example.com?Subject=Hello&body=Hi!')
uri.dump
scheme: "mailto"
to: "leonardr@example.com"
headers: [["Subject", "Hello"], ["body", "Hi!"]]
scheme: "mailto"
to: "leonardr@example.com"
headers: [["Subject", "Hello"], ["body", "Hi!"]]
puts uri.to_mailtext
To: leonardr@example.com
Subject: Hello

Hi!
To: leonardr@example.com
Subject: Hello

Hi!
URI::parse("ldap://ldap.example.com").dump
scheme: "ldap"
host: "ldap.example.com"
port: 389
dn: nil
attributes: nil
scope: nil
filter: nil
extensions: nil
scheme: "ldap"
host: "ldap.example.com"
port: 389
dn: nil
attributes: nil
scope: nil
filter: nil
extensions: nil
URI::parse('ldap://ldap.example.com/o=Alice%20Exeter,c=US?extension').dump
scheme: "ldap"
host: "ldap.example.com"
port: 389
dn: "o=Alice%20Exeter,c=US"
attributes: "extension"
scope: nil
filter: nil
extensions: nil
scheme: "ldap"
host: "ldap.example.com"
port: 389
dn: "o=Alice%20Exeter,c=US"
attributes: "extension"
scope: nil
filter: nil
extensions: nil
uri = URI.parse('tag:example.com,2006,my-tag')
uri.scheme
"tag" "tag"
uri.opaque
"example.com,2006,my-tag" "example.com,2006,my-tag"
uri = URI.parse('http://www.example.com/#anchor')
uri.port = 8080
uri.to_s
"http://www.example.com:8080/#anchor" "http://www.example.com:8080/#anchor"