# Why does this work?

**URL:** <https://www.ruby-forum.com/t/why-does-this-work/51479>\
**Category:** Ruby\
**Created:** [January 1, 2006, 12:45am UTC](https://www.ruby-forum.com/t/why-does-this-work/51479 "2006-01-01T00:45:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Garth\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/85f322/32.png) [@Garth\_W](https://www.ruby-forum.com/u/Garth_W)\
**Post date:** [January 1, 2006, 12:45am UTC](https://www.ruby-forum.com/t/why-does-this-work/51479/1 "2006-01-01T00:45:24Z")

</div>

Hi,

thread = Thread.new(thread) do |thisThread|  
# thisThread.exit  
puts “object id = #{thisThread.object\_id}”  
end

The code above seems to work, thisThread is the same as thread  
(proved by uncommenting out the line), however in most languages this  
would not work (I would expect thisThread to be nil), why does it  
work in ruby and is it considered good practice?

Also is there a better way to access the current thread?

Thanks.

Garth.

---

<div class="post-metadata">

**Author:** ![Garth\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/85f322/32.png) [@Garth\_W](https://www.ruby-forum.com/u/Garth_W)\
**Post date:** [January 1, 2006, 2:18am UTC](https://www.ruby-forum.com/t/why-does-this-work/51479/2 "2006-01-01T02:18:35Z")

</div>

On Sun, 01 Jan 2006 00:42:46 +0100, Garth W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> is it considered good practice?  
> It doesn’t work:

this\_thread is nil, so this\_tread.exit just calls the private method  
Kernel#exit with a receiver, this is not allowed, so an exception is  
thrown and the thread terminates, but you don’t see the exception. The  
following code should make it clear:

thread = Thread.new(a = thread) do |this\_thread|  
puts “object id = #{this\_thread.object\_id}”  
puts “thread id = #{Thread.current.object\_id}”  
begin  
this\_thread.exit  
rescue Exception =\> e  
p e  
end  
end  
p thread.object\_id  
p a.object\_id  
p a

Output:  
object id = 4  
thread id = -604525186  
#\<NoMethodError: private method `exit’ called for nil:NilClass\>  
-604525186  
4  
nil

Code like

x = x + 1

without defining x before this line works, because after the parser saw  
“x  
=”, it knows that x is a variable, so “x” later returns nil (which seems  
to be the default value for an uninitialized variable).

The above code results in:

irb(main):027:0\> x = x + 1  
NoMethodError: undefined method `+’ for nil:NilClass  
from (irb):27  
from :0

> Also is there a better way to access the current thread?

Thread.current (see above)

Dominik

---

<div class="post-metadata">

**Author:** ![Garth\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/85f322/32.png) [@Garth\_W](https://www.ruby-forum.com/u/Garth_W)\
**Post date:** [January 1, 2006, 11:31am UTC](https://www.ruby-forum.com/t/why-does-this-work/51479/3 "2006-01-01T11:31:19Z")

</div>

Thanks Dominik,

That explains everything.

Garth.
