Home arrow Ruby-on-Rails arrow Ruby Operators and Arrays
RUBY-ON-RAILS

Ruby Operators and Arrays


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.

Author Info:
By: James Payne
Rating: 4 stars4 stars4 stars4 stars4 stars / 5
October 16, 2007
TABLE OF CONTENTS:
  1. · Ruby Operators and Arrays
  2. · Running for Precedence
  3. · Hashes
  4. · Home on the Range

print this article
SEARCH DEVARTICLES

Ruby Operators and Arrays
(Page 1 of 4 )

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).


blog comments powered by Disqus
RUBY-ON-RAILS ARTICLES

- Ruby-on-Rails Faces Second Security Flaw in ...
- Ruby 2.0 Prepped for February 2013 Release
- Why LinkedIn Switched from Ruby on Rails
- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials