Ruby-on-Rails
  Home arrow Ruby-on-Rails arrow Ruby Operators and Arrays
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
RUBY-ON-RAILS

Ruby Operators and Arrays
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2007-10-16

    Table of Contents:
  • Ruby Operators and Arrays
  • Running for Precedence
  • Hashes
  • Home on the Range

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


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

    More Ruby-on-Rails Articles
    More By James Payne


       · Thanks for dropping by to read this installment of my series on Ruby. This...
     

    RUBY-ON-RAILS ARTICLES

    - Iterating and Incrementing Strings in Ruby
    - Comparing and Manipulating Strings in Ruby
    - Strings in Ruby
    - Ruby On Rails: Making Your First Dynamic Site
    - Ruby on Rails: Beginning Rails
    - Ruby: Modules, Mixins, Fixins, and Rails
    - Controlling Information Access with the Rail...
    - URLs, Filters and the Rails Action Controller
    - Flash and the Rails Action Controller
    - Rails Action Controller
    - Dropping and Sorting with AJAX and script.ac...
    - Drag and Drop with script.aculo.us and Rails
    - Introducing script.aculo.us
    - Ruby Classes and Objects
    - Ruby Loops







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek