w3resource

Ruby defined operator

defined? operator

The defined? the operator is used to check whether or not the passed expression is defined. It returns nil if its argument is not defined, otherwise, it returns a descriptive string of that argument.

Syntax :

defined? expression 

Here are some usage of defined? operator:

Example: Ruby defined operator

x =  1
Height = 100
puts ("defined operator in Ruby")
puts defined? x               # "local-variable"
puts defined? 12              # "expression"
puts defined? y               # nil
puts defined? puts            # "method"
puts defined? Height          # "constant"
puts defined? $&              # nil
puts defined? $_              # "global-variable"
puts defined? Math::PI        # "constant"
puts defined?( c = 22 )       # "assignment"
puts defined? 12.abs          # "method"

Output:

defined operator in Ruby
local-variable
expression

method
constant

global-variable
constant
assignment
method

Previous: Ruby Ternary operator
Next: Ruby Dot And Double Colon Operators



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/ruby/ruby-defined-operators.php