Simplify the 'defined?' API (RCR 3)

Submitted by: Wilson Bilkovich

This is the revision of Wed Jan 03 16:02:28 EST 2007

See also the current version


Problem: Based on a quick count, there are 26 possible return values from the defined? keyword. This complicates the grammar and adds unnecessary complexity to the API. Solution: Change 'defined?' to return true or false, rather than a string or nil.


After examining every usage of 'defined?' in Core, Stdlib, and all the gems I have installed (including Rails), these 26 return values are never used in any role other than 'true' or 'false'. This additional information goes unused, but represents a significant burden for Ruby implementors. The necessary code changes are straightforward, and I will happily provide a patch. A rough estimate is that this version of the 'defined?' API could be implemented with 30 or 40% of the code that the existing version requires. Some of the return values of 'defined?' make distinctions that are not normally exposed in Ruby programs, such as local variables vs. local variables in blocks as separate concepts.


This requires a language-level change because "defined?" is not a method call. It is a node in the Ruby grammar itself.


If there is interest, I can produce a patch for "is_defined" in eval.c.

Return to top

Copyright © 2006, Ruby Power and Light, LLC