ruby-changes:26830
From: drbrain <ko1@a...>
Date: Sat, 19 Jan 2013 09:55:26 +0900 (JST)
Subject: [ruby-changes:26830] drbrain:r38882 (trunk): * doc/syntax/miscellaneous.rdoc: Added section on defined?
drbrain 2013-01-19 09:52:57 +0900 (Sat, 19 Jan 2013) New Revision: 38882 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=38882 Log: * doc/syntax/miscellaneous.rdoc: Added section on defined? Modified files: trunk/ChangeLog trunk/doc/syntax/miscellaneous.rdoc Index: doc/syntax/miscellaneous.rdoc =================================================================== --- doc/syntax/miscellaneous.rdoc (revision 38881) +++ doc/syntax/miscellaneous.rdoc (revision 38882) @@ -53,6 +53,36 @@ You may undef multiple methods: https://github.com/ruby/ruby/blob/trunk/doc/syntax/miscellaneous.rdoc#L53 You may use +undef+ in any scope. See also Module#undef_method +== +defined?+ + ++defined?+ is a keyword that returns a string describing its argument: + + p defined?(UNDEFINED_CONSTANT) # prints nil + p defined?(RUBY_VERSION) # prints "constant" + p defined?(1 + 1) # prints "method" + +You don't need to use parenthesis with +defined?+ but they are recommended due +to the {low precedence}[rdoc-ref:syntax/precedence.rdoc] of +defined?+. + +For example, if you wish to check if an instance variable exists and that the +instance variable is zero: + + defined? @instance_variable && @instance_variable.zero? + +This returns <code>"expression"</code> which is not what you want if the +instance variable is not defined. + + @instance_variable = 1 + defined?(@instance_variable) && @instance_variable.zero? + +Adding parentheses when checking if the instance variable is defined is a +better check. This correctly returns +nil+ when the instance variable is not +defined and +false+ when the instance variable is not zero. + +Using the specific reflection methods such as instance_variable_defined? for +instance variables or const_defined? for constants is less error prone than +using +defined?+. + == +BEGIN+ and +END+ +BEGIN+ defines a block that is run before any other code in the current file. Index: ChangeLog =================================================================== --- ChangeLog (revision 38881) +++ ChangeLog (revision 38882) @@ -1,3 +1,7 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Sat Jan 19 09:52:46 2013 Eric Hodel <drbrain@s...> + + * doc/syntax/miscellaneous.rdoc: Added section on defined? + Sat Jan 19 09:27:31 2013 Eric Hodel <drbrain@s...> * doc/syntax/assignment.rdoc (Local Variables and Methods): Made it -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/