Forum: Ruby file name and content not corresponding

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Tom B. (Guest)
on 2007-06-17 14:16
(Received via mailing list)
Hi folks,

The code below almost works perfect. Thus it creates the text files with
the
right content. However, the name of the text file should correspond to
the
Bugtraq ID. As of now it's Bugtraq ID 1 and 3.txt, 2-2, and Bugtraq ID 3
-
1.txt. What do I have to correct? Is it index - 1?

Thanks,
Tom


# Initialize Logger
require 'logger'
root_logger = Logger.new 'root.log'
root_logger.level = Logger::INFO

# Setup at_exit hook
at_exit { root_logger.info 'done.' }

%w{ rubygems hpricot open-uri }.each { |lib| require lib }
URL_MASK =
'http://www.securityfocus.com/bid/%i'.freeze<http:/...

tgroup = ThreadGroup.new

root_logger.info 'Parsing SecurityFocus...'
( 1 .. 3 ).each do |id|
  root_logger.info "  open #{ URL_MASK % id }"
  thread = Thread.new do
    begin
      Hpricot open( URL_MASK % id )
    rescue => error
      root_logger.error error.inspect
    end
  end
  tgroup.add thread
end

tgroup.list.each_with_index do |thread, index|
  if doc = thread.value
    File.open( "#{ index + 1 }.txt", 'w+' ) do |file|
      file << ( doc/'#vulnerability' ).inner_text
    end
  end
end
Jano S. (Guest)
on 2007-06-18 13:59
(Received via mailing list)
On 6/17/07, Tom B. <removed_email_address@domain.invalid> wrote:
> Hi folks,
>
> The code below almost works perfect. Thus it creates the text files with the
> right content. However, the name of the text file should correspond to the
> Bugtraq ID. As of now it's Bugtraq ID 1 and 3.txt, 2-2, and Bugtraq ID 3 -
> 1.txt. What do I have to correct? Is it index - 1?
>
> Thanks,
> Tom

What about more explicit id using thread-local storage:
...
>     end
>   end
+   thread[:bug_id] = id
>   tgroup.add thread
> end
>
- tgroup.list.each_with_index do |thread, index|
+ tgroup.list.each do |thread|
>   if doc = thread.value
-     File.open( "#{ index + 1 }.txt", 'w+' ) do |file|
+     File.open( "#{thread[:bug_id]}.txt", 'w+' ) do |file|
This topic is locked and can not be replied to.