defined? in ruby-1.9

D

Dmitry V. Sabanin

Hi,

Lately I was converting some of my projects to ruby 1.9, and I found that this code is no longer working:
raise if defined?(Test::Unit)
Throws:
uninitialized constant Test (NameError)

Ruby 1.8 silently works with that code. So, how do I need to handle this kind of checks in 1.9?
For now as a workaround I changed Test::Unit to Test and it works fine.
 
R

Robert Klemme

Dmitry V. Sabanin said:
Hi,

Lately I was converting some of my projects to ruby 1.9, and I found
that this code is no longer working:
raise if defined?(Test::Unit)

I guess you intended to write

raise unless defined? Test::Unit
Throws:
uninitialized constant Test (NameError)

Ruby 1.8 silently works with that code. So, how do I need to handle this kind of checks in 1.9?
For now as a workaround I changed Test::Unit to Test and it works fine.

Why do you test and raise at all? As you see, Ruby throws an exception
anyway so why do it yourself? That's one line of code you can spare.

Regards

robert
 
D

Dmitry V. Sabanin

that this code is no longer working:

I guess you intended to write

raise unless defined? Test::Unit
Well, no.. this code is used in exception-catching framework, so the point is to re-raise exception
if code is used for tests or to log it if it's in standalone use.
Sorry for providing non-obvious example :)
 
R

Robert Klemme

Dmitry V. Sabanin said:
Well, no.. this code is used in exception-catching framework, so the point is to re-raise exception
if code is used for tests or to log it if it's in standalone use.

You have test specific code in production code? That sounds odd at
minimum. Care to uncover some more details?

Regards

robert
 
Y

Yukihiro Matsumoto

Hi,

In message "defined? in ruby-1.9"

|Lately I was converting some of my projects to ruby 1.9, and I found that this code is no longer working:
| raise if defined?(Test::Unit)
|Throws:
| uninitialized constant Test (NameError)
|
|Ruby 1.8 silently works with that code. So, how do I need to handle this kind of checks in 1.9?
|For now as a workaround I changed Test::Unit to Test and it works fine.

Because 1.8 silently ignores NameError in the check for Test::Unit.

matz.
 
D

Dmitry V. Sabanin

point is to re-raise exception


You have test specific code in production code? That sounds odd at
minimum. Care to uncover some more details?
I agree, but there's no other way to test modules for my web-framework, because it manages
exceptions raised in modules by itself. And the only test specific option in production code I
have is that line :)
 
R

Robert Klemme

Dmitry V. Sabanin said:
I agree, but there's no other way to test modules for my web-framework, because it manages
exceptions raised in modules by itself.

Ah, I see.
And the only test specific option in production code I
have is that line :)

Swear it by Ruby's life, that it's really the only line! :) (Sorry, the
heat might have damaged my brain... ;-))
Spam Here -> (e-mail address removed)

Hehe...

robert
 
A

Ara.T.Howard

Hi,

In message "defined? in ruby-1.9"

|Lately I was converting some of my projects to ruby 1.9, and I found that this code is no longer working:
| raise if defined?(Test::Unit)
|Throws:
| uninitialized constant Test (NameError)
|
|Ruby 1.8 silently works with that code. So, how do I need to handle this kind of checks in 1.9?
|For now as a workaround I changed Test::Unit to Test and it works fine.

Because 1.8 silently ignores NameError in the check for Test::Unit.

matz.

but isn't that what 'defined?' is for? to determine if the compiler has seen
a 'name' yet?

-a
--
===============================================================================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| A flower falls, even though we love it;
| and a weed grows, even though we do not love it.
| --Dogen
===============================================================================
 
M

Mark Sparshatt

Ara.T.Howard said:
but isn't that what 'defined?' is for? to determine if the compiler
has seen
a 'name' yet?
I supposed the rational is that by running

defined?(Test::Unit)

You are checking if the Test class/module contains a constant Unit. If
Test doesn't exist then Ruby can't check this, so it throws a NameError.

The obvious, though verbose, workaround would be to do

defined?(Test) && defined(Test::Unit)
 
A

Ara.T.Howard

I supposed the rational is that by running

defined?(Test::Unit)

You are checking if the Test class/module contains a constant Unit. If
Test doesn't exist then Ruby can't check this, so it throws a NameError.

The obvious, though verbose, workaround would be to do

defined?(Test) && defined(Test::Unit)

ah - hadn't thought of that. that it better than failing silently.

cheers.

-a
--
===============================================================================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| A flower falls, even though we love it;
| and a weed grows, even though we do not love it.
| --Dogen
===============================================================================
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: defined? in ruby-1.9"

|The obvious, though verbose, workaround would be to do
|
|defined?(Test) && defined(Test::Unit)

Yes. But your example made me change my mind. In the near future 1.9
defined?(Test::Unit) will be the shorter form of your "workaround".

matz.
 
D

Dmitry V. Sabanin

I supposed the rational is that by running

defined?(Test::Unit)

You are checking if the Test class/module contains a constant Unit. If
Test doesn't exist then Ruby can't check this, so it throws a NameError.
Now I see, thanks for explanation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,150
Messages
2,570,853
Members
47,394
Latest member
Olekdev

Latest Threads

Top