Ruby-on-Rails
  Home arrow Ruby-on-Rails arrow 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  
Dedicated Servers  
Actuate Whitepapers 
VeriSign Whitepapers 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
IBM developerWorks
 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
     
    Iron Speed
     
    ADVERTISEMENT

    Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    Ruby Conditionals


    (Page 1 of 4 )

    In the last article I left you with a cliffhanger; if you are here, then it worked. We previously discussed ranges and variables, and now we are going to speak about conditionals. But first, let's touch on ranges a bit more.

    M&M's

    I don't know about you, but I've never had an M&M melt in my mouth. Maybe that's because I shovel them in my mouth and swallow them before they have a chance. But never mind that; for this article we are talking about a different kind of M&M. 

    Minimum and maximum ranges in Ruby allow you to retrieve the highest and lowest ranking of a set of data.


    Range = 1..10

    puts range.min # prints the minimum value in the range

    puts range.max # prints the maximum value in the range

    The above code would print the following to the screen:

      1

      10

    Conditionals

    You've used conditionals all your life. Remember back in high school when you were trying to get Betty Sue to be your date and you said, “I'll pay you fifty bucks!” and she said she would think about it? Well, that was a simple conditional. You were saying if I do this, you do that. Now what if she had agreed and you gave her the money and she showed up with Biff Robertson instead of you? Well, if you were smart, you would have planned on just that sort of thing happening. Here is how that plan might have looked in pseudo code:

      If Betty Sue accepts My fifty dollars

      I have a date

      Unless Betty Sue shows up with Biff Robertson

      Then I “accidentally” spill my spiked kool-aid on her dress

    In code terms, the above would be an If statement with an else clause, meaning do this if this occurs, else do something else. There are several types of Conditionals in Ruby. We'll start with the IF statement.

    More Ruby-on-Rails Articles
    More By James Payne


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

    RUBY-ON-RAILS ARTICLES

    - 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
    - Ruby Conditionals
    - Ruby Operators and Arrays
    - Ruby for the Newbie


    Iron Speed





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway