Professional Web Applications Themes

Why don't $global and instance variables need declaration? - Ruby

Sorry if the answer is obvious, but I can't find a satisfactory or more verbose explanation about this. The only thing I found is from the Ruby's User Guide: "As with globals, instance variables have the nil value until they are initialized. Instance variables of ruby do not need declaration. This implies a flexible structure of objects. In fact, each instance variable is dynamically appended to an object when it is first referenced." Could someone explain the above? How is it more flexible? Also, how do we differentiate between an unitialized global/instance variable and a variable that has explicitly been ...

  1. #1

    Default Why don't $global and instance variables need declaration?

    Sorry if the answer is obvious, but I can't find a satisfactory or more
    verbose explanation about this. The only thing I found is from the
    Ruby's User Guide:

    "As with globals, instance variables have the nil value until they are
    initialized. Instance variables of ruby do not need declaration. This
    implies a flexible structure of objects. In fact, each instance variable
    is dynamically appended to an object when it is first referenced."

    Could someone explain the above? How is it more flexible? Also, how do
    we differentiate between an unitialized global/instance variable and a
    variable that has explicitly been set to 'nil'?

    --
    dave


    David Guest

  2. #2

    Default Re: Why don't $global and instance variables need declaration?


    "David Garamond" <6.isreserved.com> schrieb im Newsbeitrag
    news:6.isreserved.com... 

    You don't need the declaration. That saves typing and also you can add
    them at runtime as you like:

    irb(main):003:0> class Foo
    irb(main):004:1> def set(name, val)
    irb(main):005:2> instance_eval "#{name} = val"
    irb(main):006:2> end
    irb(main):007:1>
    irb(main):008:1* def get(name)
    irb(main):009:2> instance_eval name
    irb(main):010:2> end
    irb(main):011:1> end
    => nil
    irb(main):012:0>
    irb(main):013:0* f=Foo.new
    => #<Foo:0x101724a0>
    irb(main):014:0> f.set "foo", "bar"
    => "bar"
    irb(main):015:0> f.get "foo"
    => "bar"
    irb(main):016:0> f.set "hello", "world"
    => "world"
    irb(main):017:0> f.instance_variables
    => ["hello", "foo"]
    irb(main):018:0>

    If you would need to declare globals then you either need to include a
    declaration for each global you need or you had to add code in each place
    that would do the declaration on the fly when needed. That's tedious and
    error prone.
     

    Typically you don't. There are certain situations where you can use
    "defined?" but these are rare. AFAIK it's seldom used. The more usable
    option is to use "global_variables":

    irb(main):002:0> puts global_variables
    $-w
    $stderr
    $LOAD_PATH
    $<
    $-0
    $+
    $-p
    $$
    $-i
    $deferr
    $~
    $!
    $-v
    $stdout
    $_
    $-K
    $-F
    $SAFE
    $*
    $-I
    $/
    $'
    $-d
    $FILENAME
    $defout
    $
    $VERBOSE
    $stdin
    $.
    $KCODE
    $;
    $binding
    $0
    $:
    $,
    $`
    $DEBUG
    $>
    $-a
    $"
    $\
    $=
    $-l
    $?
    $&
    => nil
    irb(main):003:0>

    Regards

    robert

    Robert Guest

  3. #3

    Default Re: Why don't $global and instance variables need *initialization*?

    Robert Klemme wrote: 
    >
    > You don't need the declaration. That saves typing and also you can add
    > them at runtime as you like:[/ref]

    Oops, my bad English strikes again. The question should have been: "why
    don't $global and instance need *initialization* (i.e. they are already
    preset to nil)?" In other words, why does it behave like this?


    irb(main):001:0> a
    NameError: undefined local variable or method `a' for main:Object
    from (irb):1
    irb(main):002:0> a=nil; a
    => nil
    irb(main):003:0> $a
    => nil
    irb(main):004:0> a
    => nil


    ... and not like this?


    irb(main):001:0> a
    NameError: undefined local variable or method `a' for main:Object
    from (irb):1
    irb(main):002:0> a=nil; a
    => nil
    irb(main):003:0> $a
    NameError: undefined global variable `$a'
    from (irb):3
    irb(main):004:0> $a=nil; $a
    => nil
    irb(main):005:0> a
    NameError: undefined instance variable `a'
    from (irb):5
    irb(main):006:0> a=nil; a
    => nil

    --
    dave


    David Guest

  4. #4

    Default Re: Why don't $global and instance variables need *initialization*?

    >>>>> "D" == David Garamond <6.isreserved.com> writes:

    D> NameError: undefined local variable or method `a' for main:Object
    ^^^^^^^^^^^^^^^^^^^^^^^^

    The message is important : ruby don't know if it's a variable or a method
    and can't initialize it.


    Guy Decoux


    ts Guest

Similar Threads

  1. how to force variables declaration??
    By Mack Paul in forum PHP Development
    Replies: 3
    Last Post: September 3rd, 03:00 PM
  2. Replies: 2
    Last Post: October 8th, 08:39 PM
  3. Declaration variables
    By Justin in forum ASP.NET General
    Replies: 2
    Last Post: August 27th, 03:23 PM
  4. Global Variable declaration
    By Yarden Sheffer in forum Macromedia Director Lingo
    Replies: 5
    Last Post: July 23rd, 08:44 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139