Reputation: 168169
How can I define Thread#initialize
? I tried the following:
(1)
class Thread
def initialize
super
@foo = []
end
end
Thread.new{}.join
(2)
class Thread
def initialize &pr
super(&pr)
@foo = []
end
end
Thread.new{}.join
(3)
class Thread
def initialize
end
end
Thread.new{}.join
but they return an error:
Uninitialized thread - check `Thread#initialize'.
Upvotes: 1
Views: 155
Reputation: 7540
You're destroying the original Thread.initialize when open up the class this way, the call to super is Object.initialize, not to what was previously in Thread.initialize.
It's similar to what is happening here:
class Dog
def initialize
puts 'Arf'
end
end
class Dog
def initialize
super
puts 'I did not arf'
end
end
#dog.new "I did not arf"
You could subclass Thread and then use super, or you could use alias_method
class Thread
alias_method :old_initialize, :initialize
def initialize(*args,&blk)
puts 'Before initialize'
old_initialize(*args,&blk)
end
end
Upvotes: 2