Simple conditional causing hanging?

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

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.

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#===

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.

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.

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 :slight_smile:

David

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?

Ryan D. wrote:

Ah, I’d actually wondered about that.

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?