Ruby Operators

From Techotopia
Revision as of 20:32, 18 November 2007 by Neil (Talk | contribs)

Jump to: navigation, search

In this chapter we will cover the basics of using operators in Ruby to build expressions. Ruby provides a number of different types of operators including:

  • Assignment Operators
  • Math Operators
  • Comparison Operators
  • Bitwise Operators

The Anatomy of a Ruby Operation

In Ruby, as with most other programming languages, operations consist of values on which the calculation is to be performed (called operands) and an operator which dictates the mathematical operation to be performed. Typically, the operands are placed either side of the operator. Optionally, the assignment operator (=) can be used to assign the result of the operation to, for example, a variable. Let's take the most basic of operations, executed in irb:

irb(main):036:0> 1 + 1
=> 2
irb(main):037:0> result = 1 + 1
=> 2

Now let's assign the result to a variable called result:

irb(main):037:0> result = 1 + 1
=> 2

Performing Ruby Arithmetic using Operators

Ruby provides a number of basic operators for performing arithmetic. These are as follows:

OperatorDescription
+Addition - Adds values on either side of the operator
-Subtraction - Subtracts right hand operand from left hand operand
*Multiplication - Multiplies values on either side of the operator
/Division - Divides left hand operand by right hand operand
%Modulus - Divides left hand operand by right hand operand and returns remainder
**Exponent - Performs exponential calculation on operators

Note that when performing division, if you do not want the result to be truncated to a round number, one of the operands in the operation must be epxressed as a float:

irb(main):001:0> 10 / 7
=> 1

In the above example sthe result as been rounded down to the nearest whole number. On the otehr hand, if we express one of the operands as a float, we get a more accurate answer:

irb(main):003:0> 10.0 / 7
=> 1.42857142857143

Ruby Assignment Operators

Earlier in this chapter we looked the basic assignment operator (=) which allows us to assign the result of an expression, for example y = 10.

The '=' assignment operator does not make any changes to the value before it is assigned to the variable. A number of assignment operators are available, however, that perform arithmetic on the value before assigning it to the variable. These are essentially combined arithmetic and assignment operators. The most common operators of this type, and their "long hand" equivalents are shown below:

Combined OperatorEquivalent
x += yx = x + y
x -= yx = x - y
x /= yx = x / y
x *= yx = x * y
x %= yx = x % y

These combined operators provide a short way of assigning the results of arithmetic expressions between two variables or a variable and a value and having the result assigned to the first variable. For example:

x = 10
x += 5 # Assigns a value of 15 to variable x (the same as x = x + 5)

y = 20
y -= 10  # Assigns a value of 10 to variable y (the same as y = y - 10)

x = 10;
y = 5;

x /= y; # Assigns a value of 2 to variable x (the same as x = x / y)