Want to check if a variable is defined in Ruby? Use the defined? keyword (documentation). It will return a String with the kind of the item, or nil if it doesn’t exist.
1 2 3 4 5 6 7 8 9 10 11 12 | >> a = 1 => 1 >> defined? a => "local-variable" >> defined? b => nil >> defined? nil => "nil" >> defined? String => "constant" >> defined? 1 => "expression" |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.