Defined? Keyword in Ruby
Ruby provides a special keyword which is known as define? keyword. This keyword is used to check if the passed expression is defined or not. This keyword will return a string describing its expression or argument if the passed expression or argument is defined. Otherwise, it will return nil if the pass expression or argument is not defined. With the help of this keyword, you can check if your variable, class, method, local method, the expression is defined or not.
Let us discuss this concept with the help of the below examples:
In this example, we check if the variable is defined or not.
Result 1: local-variable Result 2: Result 3: local-variable Result 4: constant
Result 1: method Result 2: Result 3: method