How can I check whether a variable is defined in Ruby? Is there an isset
-type method available?
相关问题
- C# how to invoke a field initializer using reflect
- How to specify memcache server to Rack::Session::M
- Why am I getting a “C compiler cannot create execu
- reference to a method?
- ruby 1.9 wrong file encoding on windows
相关文章
- Ruby using wrong version of openssl
- Difference between Thread#run and Thread#wakeup?
- how to call a active record named scope with a str
- “No explicit conversion of Symbol into String” for
- Segmentation fault with ruby 2.0.0p247 leading to
- Are GetCallingAssembly() and GetExecutingAssembly(
- How to detect if an element exists in Watir
- uninitialized constant Mysql2::Client::SECURE_CONN
Use
defined? YourVariable
Keep it simple silly .. ;)
You can try:
Because it returns a boolean.
Please note the distinction between "defined" and "assigned".
x is defined even though it is never assigned!
Try "unless" instead of "if"
Use the
defined?
keyword (documentation). It will return a String with the kind of the item, ornil
if it doesn’t exist.As skalee commented: "It is worth noting that variable which is set to nil is initialized."
The correct syntax for the above statement is:
substituting (
var
) with your variable. This syntax will return a true/false value for evaluation in the if statement.