# Day6 : Ruby - Operators

## Unary operators

Unary operators expect a single operand to run on, as shown below:

Operator | Description |

! | Boolean NOT |

~ | Bitwise complement |

+ | Unary plus |

Now, let's take a look at how these operators work with an example. Create a ** koresol.com-unary_operator.rb** and write the following code in it:

**Example :**

```
puts ("Unary Operator")
puts (~4)
puts (~-4)
puts (!true)
puts (!false)
```

**Output :** Type following command on command prompt and view output

```
$ ruby koresol.com-unary_operator.rb
Unary Operator
-5
3
false
true
```

## Airthmetic operators

Arithmetic operators take numerical values as operands and return a single value.

Operator | Description |

+ | Adds values from both sides of the operator. |

- | Subtract values from both sides of the operator. |

/ | Divide left side operand with right side operand |

* | Multiply values from both sides of the operator. |

** | Right side operand becomes the exponent of left side operand. |

% |
Divide left side operand with right side operand returning remainder. |

In file ** koresol.com-arithmetic_operator.rb**, write the following code.

**Example :**

```
Path =>/koresol.com-arithmetic_operator.rb
```

```
puts ("add operator")
puts (15 + 20)
puts ("subtract operator")
puts (40 - 15)
puts ("multiply operator")
puts (5 * 8)
puts ("division operator")
puts (36 / 6)
puts ("exponential operator")
puts (7 ** 2)
puts ("modulo operator")
puts (26 % 4)
```

**Output :** Type following command on command prompt and view output

```
$ ruby koresol.com-arithmetic_operator.rb
add operator
35
subtract operator
25
multiply operator
40
division operator
6
exponential operator
49
modulo operator
2
```

## Comparison operators

Comparison operators compare two operands. Below mentioned are Ruby Comparison operators:

Operator | Description |

== |
Equal operator |

!= | Not equal operator |

> | left operand is greater than right operand |

< | Right operand is greater than left operand |

>= | Left operand is greater than or equal to right operand |

<= | Right operand is greater than or equal to left operand |

<=> |
Combined comparison operator |

.eql? | Checks for equality and type of the operands |

equal? | Checks for the object ID |

**Example:**

```
Path => /koresol.com-comparison_operator.rb
```

```
puts ("Comparison Operator")
puts (5==3)
puts (5!=3)
puts (5>3)
puts (5<3)
puts (5>=3)
puts (5<=3)
```

**Output :** Type following command on command prompt and view output

```
$ ruby koresol.com-comparison_operator.rb
false
true
true
false
true
false
```

# Day6 : Ruby - Operators

## Unary operators

Unary operators expect a single operand to run on, as shown below:

Operator | Description |

! | Boolean NOT |

~ | Bitwise complement |

+ | Unary plus |

Now, let's take a look at how these operators work with an example. Create a ** koresol.com-unary_operator.rb** and write the following code in it:

**Example :**

```
puts ("Unary Operator")
puts (~4)
puts (~-4)
puts (!true)
puts (!false)
```

**Output :** Type following command on command prompt and view output

```
$ ruby koresol.com-unary_operator.rb
Unary Operator
-5
3
false
true
```

## Airthmetic operators

Arithmetic operators take numerical values as operands and return a single value.

Operator | Description |

+ | Adds values from both sides of the operator. |

- | Subtract values from both sides of the operator. |

/ | Divide left side operand with right side operand |

* | Multiply values from both sides of the operator. |

** | Right side operand becomes the exponent of left side operand. |

% |
Divide left side operand with right side operand returning remainder. |

In file ** koresol.com-arithmetic_operator.rb**, write the following code.

**Example :**

```
Path =>/koresol.com-arithmetic_operator.rb
```

```
puts ("add operator")
puts (15 + 20)
puts ("subtract operator")
puts (40 - 15)
puts ("multiply operator")
puts (5 * 8)
puts ("division operator")
puts (36 / 6)
puts ("exponential operator")
puts (7 ** 2)
puts ("modulo operator")
puts (26 % 4)
```

**Output :** Type following command on command prompt and view output

```
$ ruby koresol.com-arithmetic_operator.rb
add operator
35
subtract operator
25
multiply operator
40
division operator
6
exponential operator
49
modulo operator
2
```

## Comparison operators

Comparison operators compare two operands. Below mentioned are Ruby Comparison operators:

Operator | Description |

== |
Equal operator |

!= | Not equal operator |

> | left operand is greater than right operand |

< | Right operand is greater than left operand |

>= | Left operand is greater than or equal to right operand |

<= | Right operand is greater than or equal to left operand |

<=> |
Combined comparison operator |

.eql? | Checks for equality and type of the operands |

equal? | Checks for the object ID |

**Example:**

```
Path => /koresol.com-comparison_operator.rb
```

```
puts ("Comparison Operator")
puts (5==3)
puts (5!=3)
puts (5>3)
puts (5<3)
puts (5>=3)
puts (5<=3)
```

**Output :** Type following command on command prompt and view output

```
$ ruby koresol.com-comparison_operator.rb
false
true
true
false
true
false
```