Ruby have several operations to perform by using Ruby Number which can perform several basic operations, some operations are listed below.
- Adding, Subtracting and Multiplying
- Division vs Modulus
- Equality Comparison
Adding, Subtracting and Multiplying
User can perform Basic mathematical operations in Ruby are quite simple user need to add two integers together just by using the
'+' operator as shown in below code.
[ruby]
irb :001 > 1 + 1
=> 2
[/ruby]
User can perform Basic mathematical operations in Ruby are quite simple user can subtract two integers together just by using the
'-' operator as shown in below code.
[ruby]
irb :001 > 1 - 1
=> 0
[/ruby]
User can perform Basic mathematical operations in Ruby are quite simple user can multiply two integers together just by using the
'*' operator as shown in below code.
[ruby]
irb :001 > 4 * 4
=> 16
[/ruby]
Division vs Modulus
User cab divide the integers by using the
'/' operator the code below demonstrates the Division as shown below.
[ruby]
irb :001 > 16 / 4
=> 4
[/ruby]
Ruby also have an operator called as modulo operator is represent by % symbol. Which can be referred to as the reminder also. The modulo operator gives the reminder of a division operation. The code below demonstrates the Modulus a shown below.
[ruby]
irb :001 > 16 % 4
=> 0
[/ruby]
Equality Comparison
There are times when user need to check if the estimations of two objects are the same. To test the correspondence of two things user can utilize the
== operator. Which analyzes the object on the left of the
== with the object of the right and returns true or false any one. genuine and false are called
Boolean values in most programming languages. The code below demonstrates the equality comparison.
[ruby]
irb :001 > 4 == 4
=> true
irb :002 > 4 == 5
=> false
[/ruby]