Catching undefined global variable errors

G

George Moschovitis

Hello everyone,

have a look the following surprising code:

$the_key = "ley1" # a common orthographic error

a = { "key1" => 1, "key2" => 2}

unless a[$the_key]
# ruby doesnt catch this error
puts "SURPRISE!!"
end

Is there a way to catch such orthographic errors (and save
valueable debugging time)? I expected that the interpreter
would raise an Exception (undefined global or something) for
this code.

best regards,
George Moschovitis
 
H

Henrik Horneber

George said:
Hello everyone,

have a look the following surprising code:

$the_key = "ley1" # a common orthographic error

a = { "key1" => 1, "key2" => 2}

unless a[$the_key]
# ruby doesnt catch this error
puts "SURPRISE!!"
end

Is there a way to catch such orthographic errors (and save
valueable debugging time)? I expected that the interpreter
would raise an Exception (undefined global or something) for
this code.
Hi!

Well, the global _is_ defined. The orthographic error is in the value,
not the variable name. How should the compiler/interpreter know what you
plan to do with your keys for the hash? :)

regards,

Henrik
 
G

gabriele renzi

George Moschovitis ha scritto:
I expected that the interpreter
would raise an Exception (undefined global or something) for
this code.

well, the variable is defined, just defined badly :)
for what relates to hashes, You can use an hash like this:
a=Hash.new { raise 'no such key' } => {}
a['key']
RuntimeError: no such key
from (irb):1
from (irb):1:in `call'
from (irb):2:in `default'
from (irb):2:in `[]'
from (irb):2

so that the error does not silently goes on..
 
G

gabriele renzi

George Moschovitis ha scritto:
I expected that the interpreter
would raise an Exception (undefined global or something) for
this code.

well, the variable is defined, just defined badly :)
for what relates to hashes, You can use an hash like this:
a=Hash.new { raise 'no such key' } => {}
a['key']
RuntimeError: no such key
from (irb):1
from (irb):1:in `call'
from (irb):2:in `default'
from (irb):2:in `[]'
from (irb):2

so that the error does not silently goes on..
 
G

gabriele renzi

George Moschovitis ha scritto:
George Moschovitis ha scritto:
I expected that the interpreter
would raise an Exception (undefined global or something) for
this code.

well, the variable is defined, just defined badly :)
for what relates to hashes, You can use an hash like this:
a=Hash.new { raise 'no such key' } => {}
a['key']
RuntimeError: no such key
from (irb):1
from (irb):1:in `call'
from (irb):2:in `default'
from (irb):2:in `[]'
from (irb):2

so that the error does not silently goes on..
 
R

Robert Klemme

gabriele renzi said:
George Moschovitis ha scritto:
I expected that the interpreter
would raise an Exception (undefined global or something) for
this code.

well, the variable is defined, just defined badly :)
for what relates to hashes, You can use an hash like this:
a=Hash.new { raise 'no such key' } => {}
a['key']
RuntimeError: no such key
from (irb):1
from (irb):1:in `call'
from (irb):2:in `default'
from (irb):2:in `[]'
from (irb):2

so that the error does not silently goes on..

You can use #fetch for that:
IndexError: key not found
from (irb):7:in `fetch'
from (irb):7

Regards

robert
 
M

Mauricio Fernández

Sorry everyone,

my example was wrong, THIS is the surprising code:

$the_key = "key1"

a = { "key1" => 1, "key2" => 2}

unless a[$the_ley] # orthographic error !!!
# ruby doesnt catch this error
puts "SURPRISE!!"
end

the orthographic error is in the variable. Shouldnt this be catched by
the interpreter ? Any ideas ?

Try to run with -w:

batsman@tux-chan:~/src/rpa/rpa-base$ ruby -w

a = { "key1" => 1, "key2" => 2}

unless a[$the_ley] # orthographic error !!!
# ruby doesnt catch this error
puts "SURPRISE!!"
end

-:4: warning: global variable `$the_ley' not initialized
SURPRISE!!
 
B

Brian Candler

Sorry everyone,

my example was wrong, THIS is the surprising code:

$the_key = "key1"

a = { "key1" => 1, "key2" => 2}

unless a[$the_ley] # orthographic error !!!
# ruby doesnt catch this error
puts "SURPRISE!!"
end

the orthographic error is in the variable. Shouldnt this be catched by
the interpreter ? Any ideas ?

Turning warnings on catches this:

# ruby -w scriptname.rb

or

#!/usr/local/bin/ruby -w # at the top of the script

or

$VERBOSE=true

Matz has said in the past that it was a mistake to leave warnings turned off
by default :)

Regards,

Brian.
 
G

Gavin Sinclair

Thanks the $VEBOSE = true does the job!
however this exception should be raised by default (POLS anyone?)

It's not an exception, so not only should it not be raised by default,
it shouldn't be raised at all!

It's a warning, which I agree should be enabled by default.

What you probably want is to not use global variables.

the_key = 'x'
hash = { 'x' => 1, ... }
hash[the_ley] # THIS will raise an exception

Try reading one of the Ruby books to learn about different variable
types.

Gavin
 

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

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top