Defining/Deleting variables

G

GGarramuno

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

Yukihiro Matsumoto

Hi,

In message "Defining/Deleting variables"

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top