Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > what is wrong and how to write it shorter?

Reply
Thread Tools

what is wrong and how to write it shorter?

 
 
ngoc
Guest
Posts: n/a
 
      08-08-2005
#!/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
 
Reply With Quote
 
 
 
 
nobu.nokada@softhome.net
Guest
Posts: n/a
 
      08-08-2005
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


 
Reply With Quote
 
 
 
 
Vincent Foley
Guest
Posts: n/a
 
      08-08-2005
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)
}
}

 
Reply With Quote
 
Ezra Zygmuntowicz
Guest
Posts: n/a
 
      08-08-2005
#!/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
http://www.velocityreviews.com/forums/(E-Mail Removed)

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
>






 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
When using System.IO.FileStream, I write 8 bytes, then seek to the start of the file, does the 8 bytes get flushed on seek and the buffer become a readbuffer at that point instead of being a write buffer? DR ASP .Net 2 07-29-2008 09:50 AM
When using System.IO.FileStream, I write 8 bytes, then seek to the start of the file, does the 8 bytes get flushed on seek and the buffer become a readbuffer at that point instead of being a write buffer? DR ASP .Net Building Controls 0 07-29-2008 01:37 AM
How to open a new console and write something out write printf() Peter Hansen C++ 1 08-24-2003 11:49 AM
How to open a new console and write something out write printf() Peter Hansen C Programming 1 08-24-2003 11:49 AM



Advertisments