Ruby-on-Rails
  Home arrow Ruby-on-Rails arrow Page 2 - Ruby Conditionals
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 
Sun Developer Network 
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 Conditionals
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2007-10-17

    Table of Contents:
  • Ruby Conditionals
  • IF Statements
  • Else Clause
  • Unless Statements

  • 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 Conditionals - IF Statements


    (Page 2 of 4 )

    As a nerdy kid, my friend and I would make Qbasic programs whose sole purpose was to insult people. This was back before people even knew what a computer was (I'm talking an old Apple IIe and no user interface). We would program little IF Statements that would ask your name, and if you entered a certain name, it would tell you you had some serious weight issues, or that your mama was so dumb, she threw a rock at the ground and missed it. Insult gold is what I'm telling you. And all thanks to the good old If Statement.

    Try the following code:


    hammer_time = 2


    if hammer_time > 1 && hammer_time < 3 # begins IF conditional

    puts “Hammer Time!” # tells it what to do if the condition is met

    end # ends the conditional

    If you run this program, you will clearly see that it is indeed, Hammer Time.

    In the above code, we are saying that if the variable hammer_time is greater than 1 AND less than three, print the words, “Hammer Time!” to the screen. We give the clause AND by using the && operator.

    We could also use the OR (||) operator to specify that we want the value to equal this OR that. Like so:


    hammer_time = 2


    if hammer_time >1 || hammer_time < 3

    puts “Hammer Time!”

    end

    Again, since hammer_time meets the criteria, “Hammer Time!” is written to the screen.

    We could have made it even simpler had we wanted:


    hammer_time = 2


    if hammer_time > 1

    puts “Hammer Time!”

    end

    All of the above code is an example of a Boolean condition. Boolean refers to a true/false situation (or Yes/No or On/Off). In other words, it's not like those multiple choice questions you got on your test.

    More Ruby-on-Rails Articles
    More By James Payne


       · Thanks for reading this tutorial. In this installment we discuss Conditionals...
     

    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-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT