Ruby Operators and Arrays
(Page 1 of 4 )
You've heard the phrase “diamonds are a girl's best friend”? Well, rubies are a nerd's best friend, and in this tutorial I am going to show you why. We'll discuss operators, conditionals, loops, hashes, and arrays in Ruby. And that's not all.
We'll discuss if-statements and else-statements and if-this-else statements. And just for you we'll even throw in if-this-else-this-then that statements. What, those don't exist? Wait a second, who's writing this article?!
So. Let's get started, shall we?
Smooth Operator
Now that we know how to store data into variables and how to print them out, it's time to move on to some more advanced ways to manipulate them. To do that, we will have to rely on operators. You've been using them ever since grade school. But in Ruby (and most other programming languages), you really get to feast your eyes on a whole new set of operators, far too advanced for those primitive fourth-grade minds. So go ahead, look at the table below. Feast!
Operator Symbol | Operator Description |
! | Not |
- | Minus |
+ | Plus |
~ | Complement |
* | Multiply |
/ | Division |
% | Modulation |
** | Exponentials |
[] | Referencing array |
[]= | Setting an array element |
>> | Shift Right |
<< | Shift Left |
& | And (Bitwise) |
^ | Exclusive Or (XOR) (Bitwise) |
| | Or (Regular) |
"==" | Equal To |
"===" | Equality test for when clause |
!= | Not equal to |
"=-" | Regular expression pattern match |
<=> | Less than, greater than, equal to |
>= | Equal to or Greater than |
> | Greater than |
< | Less than |
<= | Equal to or less than |
"=" | Assign (normal) |
%= | Assign (modulation) |
/= | Divide and Assign |
-= | Subtract and Assign |
+= | Add and assign |
*= | Multiply and Assign |
**= | Exponent and Assign |
** | And (Logical) |
|| | Or (Logical) |
.. | Range (inclusive) |
... | Range (exclusive) |
? | If (Ternary) |
: | Else |
Not | Negation (Logical) |
And/OR | Composition (Logical) |
Defined? | True if symbol is defined |
If | Modifies Statements |
Unless | Modifies Statements |
While | Modifies Statements |
Until | Modifies Statements |
Begin/End | Begins/Ends a block statement |
That was quite some feast. Don't worry, you don't have to memorize all of those Operators (there are actually even more operators). Let's fool around with some of them.
If we wanted to calculate our wage for the week, it would go something like this...
PAYRATE=20
hours=40 # hour's really equal sixty, but hey, we're salaried
put "I made this much money before taxes:"
put PAYRATE * hours
This would print out the following:
"I made this much money before taxes:"
800
If you wanted to see what you made for the month, you could do this:
PAYRATE = 20
hours = 40
puts "I made this much money before taxes for the month:"
hours = hours * 4
puts PAYRATE * hours
This takes your hours per week and multiplies it by the number of weeks in the month. It then multiplies that number by your pay rate, and prints out the amount.
"I made this much money before taxes for the month:"
3200
There is also a shorter way to write the above code using the *= operator.
PAYRATE = 20
hours = 40
puts "I made this much money before taxes for the month:"
hours *= 4
puts PAYRATE * hours
The result (the same as our prior results):
"I made this much money before taxes for the month:"
3200
(Note: You can do the same with the +=, -=, /=, etc operators).
Next: Running for Precedence >>
More Ruby-on-Rails Articles
More By James Payne