Overview of arithmetic, logical, and relational operators in Ruby programming language.

ruby-logo

In Ruby programming language, operators are symbols that represent different operations. Ruby supports a variety of operators, including arithmetic, logical, and relational operators. Understanding how to use these operators is essential for developing efficient, reliable programs.

Arithmetic Operators

Arithmetic operators are used to perform mathematical operations on numeric values. The following are the arithmetic operators supported by Ruby:

  • Addition (+): Adds two values. For example, 2 + 3 returns 5.
  • Subtraction (“): Subtracts one value from another. For example, 5 - 2 returns 3.
  • Multiplication (“): Multiplies two values. For example, 2 * 3 returns 6.
  • Division (/): Divides one value by another. For example, 6 / 3 returns 2.
  • Modulus (%): Returns the remainder of a division operation. For example, 5 % 2 returns 1.
  • Exponentiation (*): Raises a value to a power. For example, 2 ** 3 returns 8.

Logical Operators

Logical operators are used to evaluate conditions and return true or false. The following are the logical operators supported by Ruby:

  • AND (&&): Returns true if both conditions are true. For example, true && false returns false.
  • OR (||): Returns true if either condition is true. For example, true || false returns true.
  • NOT (!): Reverses the result of a condition. For example, !true returns false.

Relational Operators

Relational operators are used to compare values and return true or false. The following are the relational operators supported by Ruby:

  • Equal to (==): Returns true if two values are equal. For example, 2 == 2 returns true.
  • Not equal to (!=): Returns true if two values are not equal. For example, 2 != 3 returns true.
  • Greater than (>): Returns true if one value is greater than another. For example, 3 > 2 returns true.
  • Less than (<): Returns true if one value is less than another. For example, 2 < 3 returns true.
  • Greater than or equal to (>=): Returns true if one value is greater than or equal to another. For example, 3 >= 3 returns true.
  • Less than or equal to (<=): Returns true if one value is less than or equal to another. For example, 2 <= 3 returns true.

These operators are essential for performing different operations in Ruby code. With this knowledge, you can create more complex programs and manipulate data with ease!

Total
0
Shares
Previous Post
ruby-logo

Setting up Ruby Environment: How to install Ruby and required tools on your machine.

Next Post
ruby-logo

Operators: Overview of arithmetic, logical, and relational operators in Ruby programming language.

Related Posts