Catching undefined global variable errors

Discussion in 'Ruby' started by George Moschovitis, Sep 14, 2004.

  1. 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
    George Moschovitis, Sep 14, 2004
    #1
    1. Advertising

  2. George Moschovitis wrote:
    > 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
    Henrik Horneber, Sep 14, 2004
    #2
    1. Advertising

  3. 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..
    gabriele renzi, Sep 14, 2004
    #3
  4. 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..
    gabriele renzi, Sep 14, 2004
    #4
  5. 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..
    gabriele renzi, Sep 14, 2004
    #5
  6. "gabriele renzi" <> schrieb im Newsbeitrag
    news:...
    > 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:

    >> a={"here" => "there"}

    => {"here"=>"there"}
    >> a.fetch "here"

    => "there"
    >> a.fetch "not there"

    IndexError: key not found
    from (irb):7:in `fetch'
    from (irb):7

    Regards

    robert
    Robert Klemme, Sep 14, 2004
    #6
  7. On Tue, Sep 14, 2004 at 07:34:52PM +0900, George Moschovitis wrote:
    > 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!!

    --
    Running Debian GNU/Linux Sid (unstable)
    batsman dot geo at yahoo dot com
    Mauricio Fernández, Sep 14, 2004
    #7
  8. On Tue, Sep 14, 2004 at 07:34:52PM +0900, George Moschovitis wrote:
    > 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.
    Brian Candler, Sep 14, 2004
    #8
  9. On Tuesday, September 14, 2004, 10:59:53 PM, George wrote:

    > 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
    Gavin Sinclair, Sep 14, 2004
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mikael Engdahl

    Catching ASP.NET errors

    Mikael Engdahl, Aug 15, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    2,377
    S. Justin Gengo
    Aug 15, 2003
  2. TonyG

    Catching Framework Errors

    TonyG, Aug 23, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    322
    MS News \(MS ILM\)
    Aug 26, 2003
  3. Mark Goldin

    Errors, errors, errors

    Mark Goldin, Jan 17, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    941
    Mark Goldin
    Jan 17, 2004
  4. darrel
    Replies:
    0
    Views:
    316
    darrel
    Sep 28, 2005
  5. yawnmoth
    Replies:
    97
    Views:
    4,700
    Bent C Dalager
    Feb 27, 2009
Loading...

Share This Page