christoforever / gmail.com wrote:
> While playing around with ruby threads I noticed that if you dont call
> super when subclassing a thread you will get an initialization error.
> So ok we throw a super in the initialize method and that clears things
> up. Unfortunately if you put super as the first line in the initialize
> method the rest of the initialize will not execute but the block which
> is part of the initial thread creation gets initialized. However if
> you put super as the last statement in the initialize method, the
> whole initialize gets executed and then the block from the thread
> creation gets executed. Does anyone else find this a bit funny? And
> what is a good practice when subclassing thread... to put super first
> or last statement in the initialize method? and is there any benefit
> for either?

You can pass a block to super, and also yield within that block:

class MyThread < Thread
   def initialize(*args)
     puts "my init before super; Thread.current = #{Thread.current}"
     super do
       yield(*args)
       puts "my init in super; Thread.current = #{Thread.current}"
     end
     puts "my init after super; Thread.current = #{Thread.current}"
   end
end

th = MyThread.new do
   puts "in block"
end

th.join

__END__

Output:

my init before super; Thread.current = #<Thread:0xb7dd4700>
in block
my init in super; Thread.current = #<MyThread:0xb7cd4288>
my init after super; Thread.current = #<Thread:0xb7dd4700>

(Note that the current thread is different for methods called within the 
block.)

-- 
       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407