Introduction
Welcome readers! We hope you are doing great.
Ruby is an open-source, high-level, general-purpose programming language that supports many programming paradigms designed by Yukihiro Matsumoto in the mid-1990s.
Today, In this blog, we will discuss Nonoperators in Ruby with proper explanation. If you want to learn about Ruby, check out our articles on Ruby. This article will give you an idea about the Nonoperators in Ruby, where to use these operators, how to use them, in what scenarios they act like nonoperators etc.
So, follow the article till the end.
Nonoperators
In Ruby, most of the operators are written using the punctuation characters. Ruby’s grammar also uses punctuation characters that are not the operators. These are called Nonoperators.
Different Nonoperators
In this section, we will show you different kinds of Nonoperators in Ruby,
Parenthesis
In Ruby, we generally use the Parenthesis optionally in the method definition, invocation and grouping to affect the order of subexpression evaluation. We can think of the method invocation as a special kind of expression than think of Parenthesis as a method-invocation operator.
Curly Braces
In Ruby, the curly braces are the alternatives to do/end in blocks. They are used in hash literals. But they don’t act as an operator in either of the cases.
Square Brackets
In Ruby, we generally use the square brackets in array literals and for querying and setting array and hash values. In that context, they are syntactic sugar for method invocation, which behaves like redefinable operators with arbitrary arity.
Semicolon(;), Comma(,) and Arrow(=>)
In Ruby, we can use the punctuation characters semicolon(;), comma(,) and arrow(=>) to separate different parts.
For example,
- The semicolon(;) is used to separate statements on the same line.
- The comma(,) is used to separate the method arguments and the elements of the array and hash literals.
- The arrow(=>) is used to separate hash keys from hash values in literal.
So, these are more like separators, not the operators, so these are the nonoperators.
Colon(:)
In Ruby, we generally use the colon(:) in the hash syntax, which is also used to prefix symbol literals. So it is not an operator.
. and ::
In Ruby, the .and :: are used in qualified names,
- It separates the name of a method from the object on which it is invoked.
- It separates the name of a constant from the module in which it is defined.
As the right-hand side is not a value but an identifier, these are not operators.
*, & and <
In Ruby, we generally use these punctuation characters *, & and < as operators in some context. But they can also be used in ways in which they are not the operators.
For example,
- If we put the * before an array in an assignment or in the method invocation expression, it expands or unpacks the array into its individual elements. Though it is sometimes called the splat operator, it is not an operator.
- In a method declaration, we can use the & before the name of the last argument. This causes any block passed to the method to be assigned to that argument. It can also be used in method invocation to pass a proc to a method as of it were a block.
- < can be used in the class definitions for specifying the superclass of a class.
The cases mentioned above *, & and < do not act like normal operators, so they are nonoperators.
Check out this article - Balanced Parentheses
Know more about Unary operator overloading in c++ in detail here.