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. Advertising

  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. Advertising

  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. Advertising

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. Smyke

    Can I write this shorter?

    Smyke, Nov 17, 2003, in forum: Javascript
    Replies:
    3
    Views:
    81
    Vjekoslav Begovic
    Nov 18, 2003
  2. Dr.Ruud
    Replies:
    1
    Views:
    171
    Ted Zlatanov
    Feb 21, 2013
  3. George Mpouras
    Replies:
    0
    Views:
    159
    George Mpouras
    Feb 17, 2013
  4. C.DeRykus
    Replies:
    0
    Views:
    141
    C.DeRykus
    Feb 19, 2013
  5. Ted Zlatanov
    Replies:
    0
    Views:
    144
    Ted Zlatanov
    Feb 21, 2013
Loading...

Share This Page