what is wrong and how to write it shorter?

Discussion in 'Ruby' started by ngoc, Aug 8, 2005.

  1. ngoc

    ngoc Guest

    #!/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
     
    ngoc, Aug 8, 2005
    #1
    1. Advertisements

  2. ngoc

    Guest

    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
     
    , Aug 8, 2005
    #2
    1. Advertisements

  3. 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)
    }
    }
     
    Vincent Foley, Aug 8, 2005
    #3
  4. #!/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


    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
    >
     
    Ezra Zygmuntowicz, Aug 8, 2005
    #4
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Edwin Knoppert

    This could be shorter tight? (String to Hex)

    Edwin Knoppert, Feb 17, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    564
    Edwin Knoppert
    Feb 17, 2006
  2. Austin
    Replies:
    7
    Views:
    9,687
    Tim Tyler
    Dec 2, 2003
  3. Richard
    Replies:
    2
    Views:
    497
    Jacqui or (maybe) Pete
    Jul 10, 2003
  4. PHP2
    Replies:
    1
    Views:
    432
    Victor Bazarov
    Aug 23, 2004
  5. Kris Thomsen
    Replies:
    2
    Views:
    153
    Kris Thomsen
    Apr 15, 2009
  6. Smyke

    Can I write this shorter?

    Smyke, Nov 17, 2003, in forum: Javascript
    Replies:
    3
    Views:
    161
    Vjekoslav Begovic
    Nov 18, 2003
  7. Replies:
    3
    Views:
    267
    Frank Seitz
    May 9, 2009
  8. Dr.Ruud
    Replies:
    1
    Views:
    254
    Ted Zlatanov
    Feb 21, 2013
Loading...