1.
Introduction
2.
Unary Operators
2.1.
Unary Minus(-) Operator
2.1.1.
Example
2.1.2.
Output
2.1.3.
Explanation
2.2.
Unary Plus(+) Operator
2.2.1.
Example
2.2.2.
Output
2.2.3.
Explanation
3.
Redefinition of Unary + and - Operators
3.1.
Example
3.2.
Output
3.3.
Explanation
4.
4.1.
What are the different Arithmetic operators present in Ruby?
4.2.
What is the ternary operator in Ruby?
4.3.
What do you mean by the parallel assignment in Ruby?
4.4.
What are the range operators in Ruby?
5.
Conclusion
Last Updated: Mar 27, 2024

Unary Plus(+) and Minus(-) Operators in Ruby

Aniket Majhi
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

Hello and Welcome, readers! We hope you are doing great.

Ruby is an open-source, high-level, general-purpose programming language designed by Yukihiro Matsumoto in the mid-1990s. If you want to learn about Ruby, check out our articles on Ruby.

Did you ever get confused between the Unary and  Binary + and - operators in Ruby? Don't worry. You are in the right place.

Today, In this blog, we will closely look into the Unary Plus(+) and Minus(-) Operators in Ruby with proper explanation and implementation. You will get a clear idea about these operators in Ruby from this article, so follow the article till the end.

So, without further ado, let's start our discussion.

Unary Operators

In Ruby, the unary operators are the operators that take only a single argument as a receiver. Boolean NOT(!)Bitwise Complement(~), Unary Plus(+) and Unary Minus(-) are some of the examples of Unary operators in Ruby.

This article will only consider the Unary Plus(+) and Unary Minus(-) operators. Don't get confused with the Binary Plus(+) and Binary(-) operators. Though they look similar, there is a considerable difference between them. The binary operators deal with two arguments, whereas the unary operators only deal with a single argument.

For example,

Unary - on 2 (-2), binary + on 1 and 2 (1 + 2)

Let's now discuss the Unary - and + operators in detail.

Unary Minus(-) Operator

In Ruby, the Unary minus(-) operator changes the sign of its numeric argument. It is not the same thing as the binary - operator. The unary - operator only deals with a single argument used as a notation for the negative numbers like - on 5 (-5), whereas the binary - operator performs subtraction between two numbers like 5 - 3.

Example

``````a = 5

# using the Unary minus operation on a
b = -a

puts("The value of b = (-a): ")
puts(b)``````

Output

``````The value of b = (-a):
-5``````

Explanation

In the above example, we can see that we defined a variable named â€śaâ€ť with a value of 5, and the unary minus(-) operation on â€śaâ€ť changes it to â€ś-aâ€ť. So the output is -5.

Unary Plus(+) Operator

The Unary Plus(+) operator in Ruby does not affect the numeric argument. It just simply returns the value of the argument as it is. Like Unary + on 5 returns 5. Simply, it does not have any effect. Rather, it is provided only for the symmetry with the unary - but we can redefine it, which we will discuss later.

As explained earlier, it is also not the same as the binary + operator. The binary + operator performs addition like 1 + 2.

The Unary + operator has a slightly higher precedence than the Unary - operator.

Example

``````a = 5

# using the Unary plus operation on a
b = +a

puts("The value of b = (+a): ")
puts(b)``````

Output

``````The value of b = (+a):
5``````

Explanation

In the above example, we can see that we defined a variable named â€śaâ€ť with a value of 5, and the unary plus(+) operation on â€śaâ€ť does not affect a. So the output is 5.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Redefinition of Unary + and - Operators

As the name in both the unary and binary versions of + and - operators are the same, it can create conflict while redefining the operator in a method. The unary version's method has a suffix of @ for this ambiguity.

The names of these unary operators in methods are -@ and +@. Below is an example of redefining the Unary + and - operator in a method.

Example

``````str = "This is Coding Ninjas"

#redefining the unary - operator
def str.-@
downcase
end

#redefining the unary + operator
def str.+@
upcase
end

p str
p -str
p +str``````

Output

``````"This is Coding Ninjas"
"this is coding ninjas"
"THIS IS CODING NINJAS"``````

Explanation

In the above example, we have defined the unary - operator(using the -@ method) to translate the receiving object(String) to lower case, and we have defined the unary + operator(using the +@ method) to translate the receiving object(String) to upper case. The output is as expected.

What are the different Arithmetic operators present in Ruby?

The different arithmetic operators present in Ruby are the followings:

• Subtraction(-)
• Multiplication(*)
• Division(/)
• Modulus(%)
• Exponent(**)

What is the ternary operator in Ruby?

The ternary operator in Ruby is a conditional expression that first evaluates an expression for true or false. Based on the evaluation's result, It executes one of the two given statements.

The syntax is as follows, (Condition)? (Statement1) : (Statement2).

If the condition is true, then statement1 will execute. Otherwise, statement2 will execute.

What do you mean by the parallel assignment in Ruby?

The parallel assignment in Ruby is a method of assigning multiple variables with a single line of code.

For example,

``````A = 1
B = 2
C = 3
# is same as
A , B , C = 1 , 2 , 3``````

We can also use the parallel assignment in swapping two variables as shown below,

``````A , B = B , A
``````

What are the range operators in Ruby?

The range operators in Ruby are used to create a sequence of successive values. The range consists of a start value and an end value, and in between these two values, we have an operator("..", "...").

The ".."(two-dot form)" creates an inclusive range, whereas "..." (three-dot form) creates a range that excludes the end value.

Like,

``````1..5 contains the sequence (1 , 2 , 3 , 4 , 5)
1...5 contains the sequence (1 , 2, 3, 4)``````

Conclusion

We started this article with a basic introduction. Then we discussed the following points:

• What are the Unary Operators in Ruby
• Unary Minus(-) Operator
• Unary Plus(+) Operator
• Redefinition of unary + and - Operators

We hope that this blog gives you some ideas regarding Unary Plus(+) and Minus(-) Operators in Ruby. If you would like to learn more, follow our articles on Bitwise NOT(~), AND(&), OR(|) and XOR(^) Operations in Ruby8 Reasons Why Ruby Should be Your First Language, Ruby and Ruby On Rails: How do they differ?, Boolean in Ruby and Nonoperators in RubyExplore our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, interview bundle, follow guided paths for placement preparations and much more.!

Do upvote this blog to help other ninjas grow.