0

I'm working through the Hard Way exercises and have encountered a runtime error when attempting to run the lesson code for ex12.

Any suggestions? Thanks in advance!

INSTRUCTIONS http://ruby.learncodethehardway.org/book/ex12.html

MY INPUT

require 'open-uri'

open("http://www.ruby-lang.org/en") do |f|
    f.each_line {|line| p line}
    puts f.base_uri     # <URI::HTTP:0x40e6ef2 URL:http://www.ruby-lang.org/en/>
    puts f.content_type # "text/html"
    puts f.charset      # "iso-8859-1"
    puts f.content_encoding # []
    puts f.last_modified    # Thu Dec 05 02:45:02 UTC 2002
end

OUTPUT =>

$ ruby ex12.rb
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/open-uri.rb:174:in `open_loop': redirection forbidden: http://www.ruby-lang.org/en -> https://www.ruby-lang.org/en (RuntimeError)
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/open-uri.rb:132:in `open_uri'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/open-uri.rb:518:in `open'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/open-uri.rb:30:in `open'
from ex12.rb:3
Jay Bobo
  • 31
  • 9

2 Answers2

2

It's an SSL-Related Redirection Issue

The stack trace gives this error:

`open_loop': redirection forbidden: http://www.ruby-lang.org/en -> https://www.ruby-lang.org/en (RuntimeError)

For whatever reason, open-uri is detecting a loop when redirecting from an http scheme to https. All you need to do to fix it is use the correct scheme in your URI when calling the Kernel#open method. For example:

open('https://www.ruby-lang.org/en')

Once that change is made, the script works fine.

Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199
0

On the same exercice, after correcting http to https, i got this :

/Users/FDS/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/net/http.rb:918:in `connect': SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed (OpenSSL::SSL::SSLError)

    from /Users/FDS/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/net/http.rb:918:in `block in connect'
    from /Users/FDS/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/timeout.rb:52:in `timeout'   
    from /Users/FDS/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/net/http.rb:918:in `connect'     
    from /Users/FDS/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/net/http.rb:862:in `do_start'    
    from /Users/FDS/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/net/http.rb:851:in `start'
    from /Users/FDS/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/open-uri.rb:313:in `open_http'
    from /Users/FDS/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/open-uri.rb:708:in `buffer_open'
    from /Users/FDS/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/open-uri.rb:210:in `block in open_loop'
    from /Users/FDS/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/open-uri.rb:208:in `catch'   
    from /Users/FDS/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/open-uri.rb:208:in `open_loop'
    from /Users/FDS/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/open-uri.rb:149:in `open_uri'
    from /Users/FDS/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/open-uri.rb:688:in `open'          
    from /Users/FDS/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/open-uri.rb:34:in `open'
    from openuri.rb:2:in `<main>'