# Simple conditional causing hanging?

**URL:** <https://www.ruby-forum.com/t/simple-conditional-causing-hanging/147323>\
**Category:** Ruby\
**Created:** [August 31, 2008, 2:17am UTC](https://www.ruby-forum.com/t/simple-conditional-causing-hanging/147323 "2008-08-31T02:17:55Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![willdeluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7cd45c/32.png) [@willdeluca](https://www.ruby-forum.com/u/willdeluca)\
**Post date:** [August 31, 2008, 2:17am UTC](https://www.ruby-forum.com/t/simple-conditional-causing-hanging/147323/1 "2008-08-31T02:17:55Z")

</div>

I have the following conditional inside a method:

if ( @thisLine.class != ‘NilClass’ )  
return @thisLine.class.to\_s  
else  
return ‘nope’  
end

Obviously, all I’m trying to do is test if @thisLine is nil or not.  
When I call this method later on from inside the program, at a point  
when I know that @thisLine is nil, it returns ‘NilClass’. This means  
that my conditional has failed; it should have returned ‘nope’.

Alright, this makes sense to me. It’s failing because I’m trying to  
compare an object of type Class (@thisLine.class) to an object of type  
String (‘NilClass’). Easy fix, right? I’ll just convert that Class  
object to a String object before comparing…

if ( @thisLine.class.to\_s != ‘NilClass’ )  
return @thisLine.class.to\_s  
else  
return ‘nope’  
end

But now, when I call this method, my program hangs. I am completely  
stumped as to why this happens. Any variations to this that I think  
WOULD work properly, also hang. In addition to the above code, I have  
also tried:

if ( @thisLine != nil )  
if ( @thisLine.class != NilClass )

I would very much appreciate it if someone could explain to me why these  
extremely simple method calls are apparently causing the program to  
hang.

Thanks!  
-Will

---

<div class="post-metadata">

**Author:** ![willdeluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7cd45c/32.png) [@willdeluca](https://www.ruby-forum.com/u/willdeluca)\
**Post date:** [August 31, 2008, 3:39am UTC](https://www.ruby-forum.com/t/simple-conditional-causing-hanging/147323/2 "2008-08-31T03:39:57Z")

</div>

On Aug 30, 2008, at 17:12 , Will Dresh wrote:

> I have the following conditional inside a method:
> 
> if ( @thisLine.class != ‘NilClass’ )  
> return @thisLine.class.to\_s  
> else  
> return ‘nope’  
> end

This code is a complete mess. Simplify Simplify Simplify.

return @thisLine ? @thisLine.class.name : ‘nope’

or even:

return if @thisLine then  
@thisLine.class.name  
else  
‘nope’  
end

tho, I will say, that returning the class name is bad design smell to  
begin with. Returning ‘nope’ as your alternative is even worse.

> 

There is _nothing_ about your code that would cause a hang by calling  
#to\_s on a class. Something else entirely is going on.

---

<div class="post-metadata">

**Author:** ![willdeluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7cd45c/32.png) [@willdeluca](https://www.ruby-forum.com/u/willdeluca)\
**Post date:** [August 31, 2008, 4:50am UTC](https://www.ruby-forum.com/t/simple-conditional-causing-hanging/147323/3 "2008-08-31T04:50:06Z")

</div>

On Aug 30, 2008, at 18:56 , Michael M. wrote:

> You want @thisLine.nil?. Alternatively, you could use @thisLine ==  
> nil, @thisLine.class == NilClass, or even @thisClass === NilClass.  
> But @thisLine.nil? is the preferred way.

NilClass === @thisLine

class must come first to use Module#===

---

<div class="post-metadata">

**Author:** ![willdeluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7cd45c/32.png) [@willdeluca](https://www.ruby-forum.com/u/willdeluca)\
**Post date:** [August 31, 2008, 4:02am UTC](https://www.ruby-forum.com/t/simple-conditional-causing-hanging/147323/4 "2008-08-31T04:02:46Z")

</div>

Will Dresh wrote:

> when I know that @thisLine is nil, it returns ‘NilClass’. This means  
> return ‘nope’  
> I would very much appreciate it if someone could explain to me why these  
> extremely simple method calls are apparently causing the program to  
> hang.
> 
> Thanks!  
> -Will

You want @thisLine.nil?. Alternatively, you could use @thisLine == nil,  
@thisLine.class == NilClass, or even @thisClass === NilClass. But  
@thisLine.nil? is the preferred way.

---

<div class="post-metadata">

**Author:** ![willdeluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7cd45c/32.png) [@willdeluca](https://www.ruby-forum.com/u/willdeluca)\
**Post date:** [August 31, 2008, 9:06am UTC](https://www.ruby-forum.com/t/simple-conditional-causing-hanging/147323/5 "2008-08-31T09:06:39Z")

</div>

On Aug 30, 2008, at 22:28 , Will Dresh wrote:

> Thank you for the solution to my problem. @thisLine.nil? worked like a  
> charm. However, I am still bewildered as to why this problem arose in  
> the first place… anyone know?

As I said before… There is NOTHING in the code given that would  
cause that. It is unrelated.

---

<div class="post-metadata">

**Author:** ![willdeluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7cd45c/32.png) [@willdeluca](https://www.ruby-forum.com/u/willdeluca)\
**Post date:** [August 31, 2008, 9:52am UTC](https://www.ruby-forum.com/t/simple-conditional-causing-hanging/147323/6 "2008-08-31T09:52:51Z")

</div>

Hi –

On Sun, 31 Aug 2008, Will Dresh wrote:

> Ryan D. wrote:
> 
> > tho, I will say, that returning the class name is bad design smell to  
> > begin with. Returning ‘nope’ as your alternative is even worse.
> 
> Yeah, I know. This is not what my program actually does… I changed the  
> return statement to the class name for debugging purposes, and my else  
> case is entirely different- I just simplified it so that I would only be  
> posting the relevant part of the code, which was the conditional.

But Class#to\_s doesn’t cause hanging, unless you’ve redefined it  
somewhere. You can’t get an accurate answer if you don’t post the code  
that’s actually causing the problem 🙂

David

---

<div class="post-metadata">

**Author:** ![willdeluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7cd45c/32.png) [@willdeluca](https://www.ruby-forum.com/u/willdeluca)\
**Post date:** [August 31, 2008, 7:31am UTC](https://www.ruby-forum.com/t/simple-conditional-causing-hanging/147323/7 "2008-08-31T07:31:36Z")

</div>

Ryan D. wrote:

> tho, I will say, that returning the class name is bad design smell to  
> begin with. Returning ‘nope’ as your alternative is even worse.

Yeah, I know. This is not what my program actually does… I changed the  
return statement to the class name for debugging purposes, and my else  
case is entirely different- I just simplified it so that I would only be  
posting the relevant part of the code, which was the conditional.

Ryan D. wrote:

> There is _nothing_ about your code that would cause a hang by calling  
> #to\_s on a class. Something else entirely is going on.

Right, that’s how I felt when it happened. But if I remove all calls to  
this method from the program, it does not hang, and like I said earlier,  
if I change the conditional to something like

if ( @thisLine.class == ‘NilClass’ )

Then the program does not hang, although the code obviously does not  
function as intended.

Thank you for the solution to my problem. @thisLine.nil? worked like a  
charm. However, I am still bewildered as to why this problem arose in  
the first place… anyone know?

---

<div class="post-metadata">

**Author:** ![willdeluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7cd45c/32.png) [@willdeluca](https://www.ruby-forum.com/u/willdeluca)\
**Post date:** [August 31, 2008, 5:08pm UTC](https://www.ruby-forum.com/t/simple-conditional-causing-hanging/147323/8 "2008-08-31T17:08:48Z")

</div>

Ryan D. wrote:

> 

Ah, I’d actually wondered about that.

---

<div class="post-metadata">

**Author:** ![willdeluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7cd45c/32.png) [@willdeluca](https://www.ruby-forum.com/u/willdeluca)\
**Post date:** [August 31, 2008, 4:59pm UTC](https://www.ruby-forum.com/t/simple-conditional-causing-hanging/147323/9 "2008-08-31T16:59:58Z")

</div>

Will Dresh wrote:

> my else case is entirely different- I just simplified it […]  
> if I change the conditional to something like
> 
> if ( @thisLine.class == ‘NilClass’ )
> 
> Then the program does not hang,

If you change the line to that, your else case never executes. So how  
much do  
you want to bet that there’s something in your real else clause that  
causes  
the hanging?
