Professional Web Applications Themes

Defining/Deleting variables - Ruby

Okay, I am trying to clarify the use of nil in Ruby. When you set a variable to nil, Ruby seems to make the variable a nil object. Now, is there a way to actually delete the variable from memory? Something similar to the difference in Perl from doing: $test = undef; #equivalent to ruby's test=nil undef $test; #this removes test from namespace. how to in ruby? Also, is there any simple construct to find if a variable is defined? In Perl, there exists the function called "defined" that will return true or false based on whether the variable is ...

  1. #1

    Default Defining/Deleting variables

    Okay, I am trying to clarify the use of nil in Ruby.

    When you set a variable to nil, Ruby seems to make the variable a nil
    object.

    Now, is there a way to actually delete the variable from memory?

    Something similar to the difference in Perl from doing:
    $test = undef; #equivalent to ruby's test=nil
    undef $test; #this removes test from namespace. how to in
    ruby?


    Also, is there any simple construct to find if a variable is defined?

    In Perl, there exists the function called "defined" that will return
    true or false based on whether the variable is defined (ie. has been
    created AND is not nil).
    This is useful to build simple constructs, such as:
    name = defined name? name : "unknown"
    and is very useful for doing on the fly constructs (ie. have a program
    write code that is later evaluated).
    GGarramuno Guest

  2. #2

    Default Re: Defining/Deleting variables

    Hi,

    In message "Defining/Deleting variables"
    on 03/12/28, GGarramuno <com> writes:

    |Now, is there a way to actually delete the variable from memory?

    No. Variables live as long as enclosing scope lives.

    |Also, is there any simple construct to find if a variable is defined?

    defined?()

    matz.


    Yukihiro Guest

  3. #3

    Default Re: Defining/Deleting variables

    On Sun, 28 Dec 2003 18:51:50 +0900, GGarramuno wrote: 
     

    Why?
     

    If a variable has not been defined, it is treated as having a nil
    value or it raises a NameError if the variable has never been seen
    before and is used on the RHS of an expression (including method
    arguments).
     

    defined? will return a string ("constant", "class variable",
    "instance-variable", "expression", or "local-variable") if a
    variable has been seen, regardless of its value.
     

    You can do this with:

    name = (defined? name and name) or "unknown"

    If you want to use && and ||, you must do:

    name = (defined?(name) && name) || "unknown"

    If you're doing it in an initialization method, though, you may want
    to consider:

    def initialize(name = nil)
    name = name || "unknown"
    end

    Rather than bothering to see if name has been defined.

    -austin
    --
    austin ziegler * ca * Toronto, ON, Canada
    software designer * pragmatic programmer * 2003.12.28
    * 13.17.35


    Austin Guest

  4. #4

    Default Re: Defining/Deleting variables


    "Austin Ziegler" <ca> wrote in message 

    "or" has very low precedence, this would be better:

    name = ((defined? name and name) or "unknown")


    Shashank Guest

  5. #5

    Default Re: Defining/Deleting variables


    On Dec 28, 2003, at 10:51, Shashank Date wrote:
     
    >
    > "or" has very low precedence, this would be better:
    >
    > name = ((defined? name and name) or "unknown")[/ref]

    Very true. Some time ago I spent considerable amount of time to finally
    realize that a statement like
    name = (defined? name and name) or "unknown"
    is treated in fact as
    (name = (defined? name and name)) or "unknown"
     

    Sincerely,
    Gennady Bystritsky




    Gennady Guest

  6. #6

    Default Re: Defining/Deleting variables

    Austin Ziegler <ca> wrote in message news:<20031228132838.011860PADD>... 

    >
    > Why?
    >[/ref]

    Couple of reasons:
    1) Memory
    While setting the variable to an empty class object would likely
    work for most cases, some scripting languages make the distinction
    so
    that the memory manager will not do more work than needed when
    either
    the memory can be reused somewhere else or when the memory will be
    reused
    by the same object to fill it back again.
    2) Avoid namespace clashes
    In the past day or so I've been playing with ruby, I read about
    ruby's
    design flaw regarding local scopes. If a variable could be removed
    from
    scope, it would perhaps be a way of addressing some potential
    pitfalls, but
    I guess not.
     
    >
    > defined?[/ref]

    Doh!


    Thanks a lot guys for answering these silly questions. I will likely
    have
    some more in the upcoming days.
    So far I am really, really impressed with ruby. I am porting some
    very
    complex Perl code (Damian Conway's Perl argument parsing library,
    probably the best one in any language I've seen) and I am astonished
    at how few changes need to be made, while at the same time it is
    improving its readability.
    GGarramuno Guest

  7. #7

    Default Re: Defining/Deleting variables

    On Mon, 29 Dec 2003 10:46:49 +0900, GGarramuno wrote: 

    This is not a reason to worry about this with Ruby. Variables aren't memory
    slots, they are "tags." Objects hold the memory slots, and aren't released
    for GC until there are no more references held to them.
     

    I don't know that I'd call it a design *flaw*, but a design *decision*
    that's being changed moving forward :)

    -austin
    --
    austin ziegler * ca * Toronto, ON, Canada
    software designer * pragmatic programmer * 2003.12.29
    * 13.25.01



    Austin Guest

Similar Threads

  1. Deleting or Modifying Session Variables in anothersession
    By drmaves in forum Coldfusion - Advanced Techniques
    Replies: 3
    Last Post: July 31st, 10:43 AM
  2. deleting session variables
    By Tim Winters in forum PHP Development
    Replies: 4
    Last Post: August 19th, 08:49 PM
  3. [PHP] deleting one cookie is deleting both..why? (tiny scripts)
    By Chris Shiflett in forum PHP Development
    Replies: 0
    Last Post: July 17th, 05:50 PM
  4. Replies: 0
    Last Post: July 17th, 12:54 PM

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