What is wrong and how to write it shorter?

#!/usr/bin/ruby
require 'net/http'
h = Net::HTTP::new('www.rubyist.net')
(1..9).each do |pages|
     pg = '/~matz/slides/oscon2005/mgp0000' + pages + '.txt'
     resp, data = h.get(pg)
     print data, "\n"
     puts 10 * '-'
end

(10..59).each do |pages_2|
     pg = '/~matz/slides/oscon2005/mgp000' + pages_2 + '.txt'
     resp, data = h.get(pg)
     print data, "\n"
     puts 10 * '-'
end

[message]
block_closure.rb:5:in `+': failed to convert Fixnum into String (TypeError)
         from block_closure.rb:5
         from block_closure.rb:4:in `each'
         from block_closure.rb:4

···

*****************************************************************

An attempt to write it shorter
#!/usr/bin/ruby
require 'net/http'
h = Net::HTTP::new('www.rubyist.net')
h.get('/~matz/slides/oscon2005/') { |pages|
     print pages, "\n"
     puts 10 * '-'
}
in theory, above code merge html and text. How to pick up text only file?

[message]
block_closure.rb:24:in `*': String can't be coerced into Fixnum (TypeError)
         from block_closure.rb:24
         from block_closure.rb:22:in `call_block'
         from /usr/lib/ruby/1.6/net/protocol.rb:342:in `<<'
         from /usr/lib/ruby/1.6/net/protocol.rb:622:in `rbuf_moveto'
         from /usr/lib/ruby/1.6/net/protocol.rb:516:in `read'
         from /usr/lib/ruby/1.6/net/http.rb:1272:in `read_body_0'
         from /usr/lib/ruby/1.6/net/http.rb:1247:in `read_body'
         from /usr/lib/ruby/1.6/net/http.rb:602:in `get'
         from /usr/lib/ruby/1.6/net/http.rb:601:in `request'
         from /usr/lib/ruby/1.6/net/http.rb:668:in `request'
         from /usr/lib/ruby/1.6/net/http.rb:667:in `start'
         from /usr/lib/ruby/1.6/net/http.rb:667:in `request'
         from /usr/lib/ruby/1.6/net/http.rb:601:in `get'
         from block_closure.rb:22

Hi,

At Tue, 9 Aug 2005 07:16:09 +0900,
ngoc wrote in [ruby-talk:151287]:

#!/usr/bin/ruby
require 'net/http'
h = Net::HTTP::new('www.rubyist.net')
(1..9).each do |pages|
     pg = '/~matz/slides/oscon2005/mgp0000' + pages + '.txt'

       pg = "/~matz/slides/oscon2005/mgp0000#{pages}.txt"

     resp, data = h.get(pg)
     print data, "\n"
     puts 10 * '-'

       puts '-' * 10

end

Or

  (1..59).each do |pages|
    pg = "/~matz/slides/oscon2005/mgp%05.txt" % pages

···

--
Nobu Nakada

#!/usr/local/bin/ruby
require 'net/http'

def self.fetch(page, limit=5)
    begin
       data = Net::HTTP.get_response("www.rubyist.net", "/#{page}")
    rescue Exception => e
       limit -= 1
       limit > 0 ? retry : raise
    end
    data.body
end

(1..9).each do |pages|
     pg = "/~matz/slides/oscon2005/mgp0000#{pages}.txt"
     puts fetch(pg)
     puts '-' * 1
end

(10..59).each do |pages|
     pg = "/~matz/slides/oscon2005/mgp000#{pages_2}.txt"
     puts fetch(pg)
     puts '-' * 1
end

HTH-
-Ezra Zygmuntowicz
Yakima Herald-Republic
WebMaster
509-577-7732
ezra@yakima-herald.com

···

On Aug 8, 2005, at 3:16 PM, ngoc wrote:

#!/usr/bin/ruby
require 'net/http'
h = Net::HTTP::new('www.rubyist.net')
(1..9).each do |pages|
    pg = '/~matz/slides/oscon2005/mgp0000' + pages + '.txt'
    resp, data = h.get(pg)
    print data, "\n"
    puts 10 * '-'
end

(10..59).each do |pages_2|
    pg = '/~matz/slides/oscon2005/mgp000' + pages_2 + '.txt'
    resp, data = h.get(pg)
    print data, "\n"
    puts 10 * '-'
end

[message]
block_closure.rb:5:in `+': failed to convert Fixnum into String (TypeError)
        from block_closure.rb:5
        from block_closure.rb:4:in `each'
        from block_closure.rb:4

*****************************************************************

An attempt to write it shorter
#!/usr/bin/ruby
require 'net/http'
h = Net::HTTP::new('www.rubyist.net')
h.get('/~matz/slides/oscon2005/') { |pages|
    print pages, "\n"
    puts 10 * '-'
}
in theory, above code merge html and text. How to pick up text only file?

[message]
block_closure.rb:24:in `*': String can't be coerced into Fixnum (TypeError)
        from block_closure.rb:24
        from block_closure.rb:22:in `call_block'
        from /usr/lib/ruby/1.6/net/protocol.rb:342:in `<<'
        from /usr/lib/ruby/1.6/net/protocol.rb:622:in `rbuf_moveto'
        from /usr/lib/ruby/1.6/net/protocol.rb:516:in `read'
        from /usr/lib/ruby/1.6/net/http.rb:1272:in `read_body_0'
        from /usr/lib/ruby/1.6/net/http.rb:1247:in `read_body'
        from /usr/lib/ruby/1.6/net/http.rb:602:in `get'
        from /usr/lib/ruby/1.6/net/http.rb:601:in `request'
        from /usr/lib/ruby/1.6/net/http.rb:668:in `request'
        from /usr/lib/ruby/1.6/net/http.rb:667:in `start'
        from /usr/lib/ruby/1.6/net/http.rb:667:in `request'
        from /usr/lib/ruby/1.6/net/http.rb:601:in `get'
        from block_closure.rb:22

require "open-uri"

BASE_URL = "http://www.rubyist.net/~matz/slides/oscon2005/mgp"
"00001".upto("00059") { |n|
  puts "Fetching #{n}.html..."
  File.open(n + ".html", "w") { |f|
    f.write(open(BASE_URL + n + ".html").read)
  }
}