Table of contents
1.
Introduction
2.
Double Splat Operator (**)
3.
Syntax
4.
Power of number using ** operator
4.1.
Program
4.1.1.
Input
4.1.2.
Output
5.
Frequently Asked Questions
5.1.
Is there operator overloading in Ruby?
5.2.
In Ruby, what are class libraries?
5.3.
Name some of the Ruby operators.
5.4.
What exactly are Ruby blocks?
6.
Conclusion
Last Updated: Mar 27, 2024

Exponentiation: ** operator in Ruby

Author Ayush Mishra
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Yukihiro Matsumoto desired a scripting language that was more powerful than Perl and more object-oriented than Python, so he created Ruby.

Ruby is an open-source, interpreted, high-level, dynamic, general-purpose programming language focusing on productivity and simplicity.

As you'd expect from a contemporary language, Ruby has many operators. In this blog, we will discuss Ruby's double splat operator(**).

Let's get started with the ** operator in Ruby.

Double Splat Operator (**)

A symbol denoting an operation performed with one or more operands is called an operator. Any programming language's base is made up of operators. We may use operators to execute a variety of actions on operands.

In Ruby, several distinct types of operators; we'll discuss the Exponentiation operator.

** operator is used to return the exponential power of the operators. For example, a**b.

It performs Exponentiation, which means it raises the first argument to the power of the second.

It's worth noting that a fractional number can be used as the second argument to compute the roots of a number. For example, the cube root of a is a**(1.0/3.0). Similarly, a**-b is the same as 1/(a**b).

Syntax

def function_name(**double_splat)
 #function body
end

Power of number using ** operator

The task is to develop a program that prints the power of a number using a double splat operator in Ruby programming language.

If we wish to manually compute a number's power, we must multiply the base by the exponent times. For example, if the base is 5 and the exponent is 3, the power will be calculated as

The answer is 125 if power = 5*5*5.

The following methods were used:

gets: This function takes user input in the form of a string.

puts: This command initiates a dialogue with the user by displaying a message on the console.

to_i: To convert any type to an integer type.

Program

=begin
power of a number using ** operator
=end

base=gets.chomp.to_i

exponent=gets.chomp.to_i

power=base**exponent
puts "The power of #{base} is #{power}"

Input

2
3

Output

The power of 2 is 8

 

Read about Bitwise Operators in C here.

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

Frequently Asked Questions

Is there operator overloading in Ruby?

Ruby has operator overloading, which allows you to specify how an operator will be used in a program.

In Ruby, what are class libraries?

Ruby class libraries cover many topics, including thread programming, data types, and other disciplines.

Name some of the Ruby operators.

Some operators used in Ruby are a unary, arithmetic, bitwise, logical operator, and ternary operator.

What exactly are Ruby blocks?

Closures are what other programming languages call Ruby code blocks. It comprises a collection of codes that are always contained in braces or written between do...end.

Conclusion

In this article, we learned about Ruby's ** operators and how to use a double splat operator to calculate the power of a number. This isn't enough, though, because there's always more to discover and understand about Ruby in depth.

Are you eager to read/explore additional articles on the subject of Ruby after reading about the double splat operator in Ruby? Don't worry; Coding Ninjas will take care of everything. See Ruby on Rails: An Introduction, Ruby Directory StructureRuby on Rails, and Ruby versus Python.

Close your eyes and visit Coding Ninjas Studio to practice top problems, take practice exams, and read interview stories, among other things.

Happy Learning!

Live masterclass