From: Robert Klemme Date: 2008-10-11T06:33:55+09:00 Subject: Re: Thread: super should be first line or last line? On 10.10.2008 15:02, 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. This is wrong. irb(main):001:0> class T < Thread irb(main):002:1> def initialize irb(main):003:2> p self irb(main):004:2> super irb(main):005:2> p self irb(main):006:2> end irb(main):007:1> end irb(main):009:0> T.new { sleep 1; p self }.join # # main => # irb(main):010:0> The code in the block gets executed. But the thread is started from super so the thread is already running when the rest of initialize is executed. One more reason why it is a bad idea to inherit Thread. If you need to store information in the thread you can always use Thread.current[:key] = value > 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? See above. If you need to do complex calculations you should rather create a class that does the work and stores all necessary information. Regards robert