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).

Hi,

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.
···

In message “Defining/Deleting variables” on 03/12/28, GGarramuno GGarramuno@aol.com writes:

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?

Why?

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

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).

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).

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

This is useful to build simple constructs, such as:
@name = defined name? name : “unknown”

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

···

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

austin ziegler * austin@halostatue.ca * Toronto, ON, Canada
software designer * pragmatic programmer * 2003.12.28
* 13.17.35

“Austin Ziegler” austin@halostatue.ca wrote in message

You can do this with:

@name = (defined? name and name) or “unknown”

“or” has very low precedence, this would be better:

@name = ((defined? name and name) or “unknown”)

Austin Ziegler austin@halostatue.ca wrote in message news:20031228132838.011860@PADD

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?

Why?

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.

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

defined?

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.

···

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

“Austin Ziegler” austin@halostatue.ca wrote in message

You can do this with:

@name = (defined? name and name) or “unknown”

“or” has very low precedence, this would be better:

@name = ((defined? name and name) or “unknown”)

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

···

On Dec 28, 2003, at 10:51, Shashank Date wrote:

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.

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.

  1. 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.

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

-austin

···

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

austin ziegler * austin@halostatue.ca * Toronto, ON, Canada
software designer * pragmatic programmer * 2003.12.29
* 13.25.01