Ruby-on-Rails
  Home arrow Ruby-on-Rails arrow Page 3 - 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  
Moblin 
JMSL Numerical Library 
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 - Else Clause


    (Page 3 of 4 )

    No, Else Clause is not the Swedish niece of old Saint Nick. In the above example, we found out what happened if the hammer_time conditional was met. But what if it wasn't hammer time? Aside from making Emanuelle Lewis very sad, what would happen?


    hammer_time = 3


    if hammer_time > 1 && hammer_time < 3

    puts “Hammer Time!”

    else

    puts “Ice Ice Baby!”

    end

    Since the hammer_time value is greater than 1 but not less than 3 (remember, with the && operator both conditions must be met), the program outputs the following to the screen:

      “Ice Ice Baby!”

    Elseif

    Elseif sounds like one of those words a little kid would say when they are angry. “Nu uh you better give me your ice cream elseif my big brother will beat you up!” That kid would grow up to be an awesome programmer, because he understands conditionals at such a young age. 

    Elseif clauses allow you to check for multiple conditions. Remember that insult program I used to make? Let's put it back in business...


    my_name = “James”


    if my_name == “James”

    puts “You are so cool and handsome and have only one chin.”

    elseif my_name == “Eric”

    puts “Your fingers are so fat I'm surprised you can type.”

    else

    puts “I don't know that name, but you're probably an idiot.”

    end

    In the above example, my_name's value is “James”, so it would print out the text “You are so cool and handsome and have only one chin.” line. Had I made the value of my_name “Eric” it would have printed the line: “Your fingers are so fat I'm surprised you can type.” And finally, if I had changed the value of my_name to anything other than “James” or “Eric”, it would have printed: “I don't know that name, but you're probably an idiot.”

    In simple terms, the program asks two questions. If the answer answers the first question, it performs one action, if not, it asks the second question. If the answer is the one it is seeking for the second question, it performs a different result. And finally, if the answer is not one the program is seeking for either question, it performs another result.

    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 1 hosted by Hostway
    Stay green...Green IT