Difference between revisions of "Ruby Operators"

From Techotopia
Jump to: navigation, search
(New page: In this chapter we will cover the basics of perfoming mathematical operations in Ruby. == The Anatomy of a Ruby Math Operation == In Ruby, as with most other programming languages, mathe...)
 
(The Anatomy of a Ruby Math Operation)
Line 3: Line 3:
 
== The Anatomy of a Ruby Math Operation ==
 
== The Anatomy of a Ruby Math Operation ==
  
In Ruby, as with most other programming languages, mathematical 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'' a replaced 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'':
+
In Ruby, as with most other programming languages, mathematical 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'':
  
 
<pre>
 
<pre>

Revision as of 21:25, 15 November 2007

In this chapter we will cover the basics of perfoming mathematical operations in Ruby.

The Anatomy of a Ruby Math Operation

In Ruby, as with most other programming languages, mathematical 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 Arithmetic using Operators

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