Fundamentals of Ruby
Learn the commonly used data types and operators in Ruby.
Data types in Ruby
Data types are classifications or categorizations of data items. They tell us what operations are often performed on specific data.
Common data types
Ruby supports three basic data types:
Integer
: Integer values (5
,0
,-1
, etc.)Float
: Floating-point values (2.33333
,-2500.001
, etc.)String
: String values (educative
,ruby
, etc.)
The following code demonstrates variables that store values of basic data types in Ruby:
persons = 5 # Defining a variable of type int to store the number of personstotalWeight = 350.6 # Defining a variable of type float to store the total weight of 5 personsteam = "A" # Defining a variable of type str to store the team name of personsprint("Total weight of #{persons} persons of team #{team} is: #{totalWeight}.\n")
The following code demonstrates the effect of the +
operation on basic data types in Ruby:
a = 5b = 7sum1 = a + b # Sum of a and bprint("The sum of integers: #{sum1}\n")e = 3.2f = 1.5sum2 = e + f # Sum of e and fprint("The sum of floats: #{sum2}\n")c = "5"d = "7"result = c + d # Concatenates c and dprint("The concatenation of strings: #{result}\n")
Every input is a string in Ruby
When we use the gets
statement in Ruby, it always returns a string of characters. Even the integer, 25
, is also considered a string, as shown in the following code widget:
print("Enter an integer: ") a = gets.chomp # 1st integer print("Enter another integer: ") b = gets.chomp # 2nd integer print("When we apply + operator it gives #{a+b} which is concatenation of two strings!\n")
In the code above, integers are treated as strings, and they are concatenated due to the +
operator in the output.
We need to convert the input value to a suitable data type to apply the correct operation. We should use the Integer
data type for our example, as demonstrated below:
print("Enter an integer: ") a = Integer(gets) # 1st integer print("Enter another integer: ") b = Integer(gets) # 2nd integer print("When we apply + operator it gives #{a+b} which is the sum of two integers!\n")
Similarly, we can use the float
data type to store a floating-point value in a variable, as demonstrated below:
print("Enter a floating-point value: ") a = Float(gets) # Taking float variable input in a print("You entered the value #{a}\n")
Note: Even if the input value is an integer,
Float
converts it into a floating point. The result will include.0
in the output.
Operators in Ruby
Like any other programming language, Ruby supports several types of operators. We have already seen the assignment (=
) operator that assigns a value to a variable in our programs. The following are various forms of the assignment operator:
a = b = 5 # Cascaded assignmentc , d = 10 , 20 # Tuple assignmentprint("a = #{a}\n")print("b = #{b}\n")print("c = #{c}\n")print("d = #{d}\n")
Line 1: We assign 5
to both a
and b
. This assignment syntax is called a cascaded or chained assignment.
Line 2: We assign 10
to c
and 20
to d
. This assignment syntax is called a tuple assignment.
Commonly used operators
There are several other commonly used operators in Ruby, including:
-
Arithmetic operators: These are used for arithmetic operations (addition, subtraction, multiplication, etc.).
-
Comparison operators: These are used in conditional expressions (less than, greater than, etc.).
-
Logical operators: These are used to combine conditional expressions (and, or, not).
-
Compound assignment operators: These are the combinations of arithmetic and assignment operators.
Arithmetic Operators
Operator | Description | Syntax | Results x=7 and y=4 |
+ | Addition: Adds two operands | x + y | 11 |
- | Subtraction: Subtracts two operands | x - y | 3 |
* | Multiplication: Multiplies two operands | x * y | 28 |
/ | Division: Divides the first operand by the second | x / y | 1 |
/ | Non-integer Division: Divides the first operand by the second and gives a floating-point quotient. A quotient is a floating-point number when the dividend or the divisor or both are floating-point numbers. | x / 4.0 | 1.75 |
% | Modulo: Returns the remainder when the first operand is divided by the second | x % y | 3 |
** | Power: Returns the first number raised to power of the second number. | x ** y | 2401 |
Comparison Operators
Operator | Description | Syntax | Results x=7 and y=4 |
> | Greater than: True if the left operand is greater than the right | x > y | True |
< | Less than: True if the left operand is less than the right | x < y | False |
== | Equal to: True if both operands are equal | x == y | False |
!= | Not equal to: True if both operands are not equal | x != y | True |
>= | Greater than or equal to: True if the left operand is greater than or equal to the right | x >= y | True |
<= | Less than or equal to: True if the left operand is less than or equal to the right | x <= y | False |
<=> | Combine comparison: Returns 0 if both variables are equal, 1 if the first is greater than the second, and -1 if the first is smaller than the second | x<=>y | 1 |
=== | Used to test equality within a when clause of a case statement | y...x===5 | True |
.eql? | True if the receiver and argument have both the same type and equal values | 1.eql?1.0 | False |
Logical Operators
Operator | Description | Syntax | Results x=7 and y=4 |
and | Logical AND: True if both the operands are true | x > 4 and y > 4 | False |
or | Logical OR: True if either of the operands is true | x > 4 or y > 4 | True |
not | Logical NOT: True if the operand is false | not x > 4 | False |
Compound Assignment Operators
Operator | Description | Syntax | Results x=7 and y=4 |
= | Assignment operator: Assigns values from the right-side operands to the left-side operands | x = y | x = 4 |
+= | Add and assign: Add the right-side operand with the left-side operand and then assign the result to the left operand (x = x + y) | x += y | x = 11 |
-= | Subtract and assign: Subtract the right-side operand from the left-side operand and then assign the result to the left operand (x = x - y) | x -= y | x = 3 |
*= | Multiply and assign: Multiply the right-side operand with the left-side operand and then assign the result to left operand (x = x * y) | x *= y | x = 28 |
/= | Divide and assign: Divide the left operand with the right operand, and then assign it to the left operand (x = x / y) | x /= y | x = 1.75 |
%= | Modulo and assign: Take the modulo after dividing the left operand with the right operand, and, then assign the result to the left operand (x = x % y) | x %= y | x = 3 |
**= | Exponent and assign: Calculate the exponent (raised power) value using operands and then assign the value to the left operand (x = x ** y) | x **= y | x = 2401 |