different handling of local variables and instance variables when undefined

Discussion in 'Ruby' started by Tammo Tjarks, Sep 12, 2007.

  1. Tammo Tjarks

    Tammo Tjarks Guest

    Hello,
    I somehow stumbled over a, how I think, strange behavior. When I try to
    handle a undefined variable in irb I get something like that:

    irb(main):001:0> !blubb
    NameError: undefined local variable or method `blubb' for main:Object
    from (irb):1

    But when I do the same with a undefined instance variable I get:
    irb(main):002:0> !@blubb
    => true

    I think that is somehow strange. Because of that I have overseen a
    mispelling in the instance variable name. Is that the wanted behavior?


    Regards,
    Tammo
     
    Tammo Tjarks, Sep 12, 2007
    #1
    1. Advertisements

  2. Tammo Tjarks

    Phrogz Guest

    On Sep 12, 3:59 pm, Tammo Tjarks <> wrote:
    > I think that is somehow strange. Because of that I have overseen a
    > mispelling in the instance variable name. Is that the wanted behavior?


    Yup, it's what I wanted.
    (Do you care if that's what I wanted?)
     
    Phrogz, Sep 13, 2007
    #2
    1. Advertisements

  3. Tammo Tjarks

    Tammo Tjarks Guest

    Sorry maybe I misformulated my question. I try it again. So my questions are
    a) What is the intention for the different handling? What are the benefits
    and purpose of it?
    b) Is it possible to change that behavior somehow, maybe with something
    like 'strict'?

    I have laready looked around and have seen, that it is the normal behavior.
    I should get used to always use the -w switch. I have also seen in
    http://rubyquiz.com/quiz23.html
    that you can enable warnings in the script itself with
    $VERBOSE = true

    But of course I get only warnings for the "active code". Now is the
    questions do I have alsway to use unit-testing and to take care to get the
    whole coverage or are there some kind of linter existing to check the whole
    code if there are some possible execution flows where some variables could
    be uninitialiyed? Propably that is relative complicated because of the
    dynamic nature of ruby, but I am not sure. I have tried to compare ruby and
    perl with respect to this. It seems, that perl recognize unizializes
    variables on toplevel also only when warnings activated. But I get somehow
    a error message when I used a unitialized variable inside a block:

    use strict;
    use warnings;


    $b = 0;
    if ( !$a ) {
    print "uses unitialized variable\n";
    }

    if ( $b ) {
    if ( !$c ){
    print "ignores that \$c is uninitialized\n";
    }
    }

    Then I get the error message (it seems caused by strict):
    Global symbol "$c" requires explicit package name at chk_var.pl line 11.
    Execution of chk_var.pl aborted due to compilation errors.

    What I do not understand to be honesty, because I have tried before that I
    get only a warning for the undefined $a.

    Anyway, in ruby I get:

    $VERBOSE = true

    $b = false
    if !$a then
    print "uses unitialized variable\n";
    end

    if $b then
    if !$c then
    puts "ignores that $c is uninitialized"
    end
    end

    And get only, when run, the following message:
    chk_var.rb:4: warning: global variable `$a' not initialized
    uses unitialized variable

    What makes sense in so far, as the interpreter never comes to the point
    where !$C is evaluated. This is a artificial example. But my questions is,
    what is the recomemended and usual used method for catching such errors in
    the coding. Of course some possibility would be to have always unit tests
    with 100% percent coverage. But I do not know if that is always possible
    and maybe there is a simpler solution. And otherwise, if somebody knows, I
    would like to know what perl means with that error message.

    I appreciate every feedback.

    Best regards,
    Tammo

    Phrogz wrote:

    > On Sep 12, 3:59 pm, Tammo Tjarks <> wrote:
    >> I think that is somehow strange. Because of that I have overseen a
    >> mispelling in the instance variable name. Is that the wanted behavior?

    >
    > Yup, it's what I wanted.
    > (Do you care if that's what I wanted?)
     
    Tammo Tjarks, Sep 13, 2007
    #3
    1. Advertisements

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. Replies:
    10
    Views:
    36,668
    jporter892
    Jun 6, 2011
  2. Seb
    Replies:
    1
    Views:
    447
    Leor Zolman
    Apr 14, 2004
  3. Sullivan WxPyQtKinter
    Replies:
    10
    Views:
    915
    Antoon Pardon
    Nov 8, 2007
  4. Eric D.
    Replies:
    3
    Views:
    301
    Jeremy Henty
    Feb 1, 2006
  5. yuesefa
    Replies:
    4
    Views:
    217
    Leslie Viljoen
    Jul 25, 2006
  6. Jason Lillywhite
    Replies:
    2
    Views:
    138
    Jason Lillywhite
    Mar 18, 2009
  7. DJB
    Replies:
    1
    Views:
    167
  8. king
    Replies:
    2
    Views:
    322
    Tad McClellan
    Jun 27, 2006
Loading...