Table of contents
1.
Introduction
2.
Some Important Terms
3.
The defined? Operator
3.1.
Example:
3.2.
Output:
4.
Frequently Asked Questions
4.1.
Is the expression passed to the defined? operator evaluated?
4.2.
What does it mean by non-associative?
4.3.
Why does the unary operator uses +@ and -@?
5.
Conclusion
Last Updated: Mar 27, 2024

The defined? Operator in Ruby

Author Gurleen Kaur
1 upvote

Introduction

The operations performed on one or more operands are represented by a token or symbol known as an operator. However, every programming language has its own set of operators used to perform multiple operations on the operands.

Operands are expressions, and operators further help to convert these expressions into larger ones. For example, if we take the numeric literal 7 and an operator *, it can be combined into an expression like 7*7. Similarly, the expression below combines a numeric literal, a method invocation expression, and a variable reference expression with the multiplication and greater-than operator.

7 * Math.sqrt(7) > limit

where 7 is a numeric literal and Math.sqrt(7) is a method invocation. We will further discuss the working of operators in detail.

Some Important Terms

Some important terms related to operators are-

1. Arity- The number of operands upon which an operator can work is called its arity. The unary operator works on only a single operand. The binary operator operates on two operands; the ternary operator works on three operands, etc.

2. Precedence- The bonding of an operator with its operands. It tells which operation is performed first and which next.

Example:

2 + 3 * 4     # => 14

In this example, we see multiplication has higher precedence than addition; hence it is performed first.

3. Associativity- If the same operator occurs multiple times in an expression and sequentially, it defines the expression's order of evaluation.

The table below describes all the operators according to their precedence from top to bottom and further shows the same's arity, associativity, and operation.

The defined? Operator

To test whether the given expression has its operand defined or not, the defined? Operator is used. It is a special unary operator. Generally, when the expression is not defined, it raises an error. But, using this operator, it returns NIL if the expression or method used on the right side of the defined? is undefined.

Defined? operator also returns NIL if the expression uses YIELD or SUPER in the wrong context, i.e., when there are no such blocks to yield to or no superclass method to invoke. It is of very low precedence.

Syntax:

# Compute f(x), however only if f and x are both defined
y = f(x) if defined? f(x)

If the given expression is correct, it returns a string. The various values it can return-

Example:

c =  7
Width = 500
puts ("The defined? operator in Ruby")
puts defined? c                       # "local-variable"
puts defined? 77                    # "expression"
puts defined? N                      # nil
puts defined? puts                 # "method"
puts defined? Width              # "constant"
puts defined? $&                    # nil
puts defined? $_                     # "global-variable"
puts defined? Math::PI           # "constant"
puts defined?( l = 22 )            # "assignment"
puts defined? 72.abs              # "method"

Output:

The defined? operator in Ruby
local-variable
expression

method
constant

global-variable
constant
assignment
method

Frequently Asked Questions

Is the expression passed to the defined? operator evaluated?

No, the expression is not evaluated. It is only used it check if it can run without errors.

What does it mean by non-associative?

The operator can’t be used multiple times in an expression using the parentheses.

Why does the unary operator uses +@ and -@?

It uses this symbol to avoid ambiguity with the binary operator having the same symbol.

Know more about Unary operator overloading in c++ in detail here.

Conclusion

In this article on Ruby, we have extensively discussed the defined? operator. We started with a brief introduction of the operators, their important terms, how to use them, and the defined? operator along with an example.

You can refer to our guided paths on Coding Ninjas Studio to train yourself in Competitive ProgrammingData Structures and AlgorithmsJavaScriptSystem Design, and many more! Also, if you want to test your competency in coding, check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! However, if you have just started your learning process and are only looking for tech giants like Amazon, Microsoft, Uber, etc. In that case, you should look at the several problems, multiple interview experiences, and interview bundles for placement preparations.

Do upvote our blogs if you find them helpful and engaging!

Happy learning!

Live masterclass