On Sun, 28 Dec 2003 18:51:50 +0900, GGarramuno wrote: > 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 -- austin ziegler * austin / halostatue.ca * Toronto, ON, Canada software designer * pragmatic programmer * 2003.12.28 * 13.17.35