Ruby-on-Rails
  Home arrow Ruby-on-Rails arrow Page 3 - Ruby Loops
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 Loops
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2007-10-22

    Table of Contents:
  • Ruby Loops
  • The While Loop
  • Stop that Loop!
  • Reach Out and Touch Someone
  • Scope

  • 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 Loops - Stop that Loop!


    (Page 3 of 5 )

    So now you have created a loop that won't quit. The loop models itself a regular Lindsey Lohan or Britney Spears and is clearly out of control. Partying all the time, forgetting articles of clothing, and forcing its utter untalents upon the world. Well, we're going to put a stop to the loopiness, quicker than an overnight stay at a celebrity rehab center.

    There are three keywords you can use with loops. Want to see them in a table? Yeah, I thought you might:


    Keyword

    What it does

    Break

    Ends the current loop it is in (if you have nested loops, it only ends the loop it is encapsulated with).

    Redo

    Without checking the loop's criteria, it repeats the current loop.

    Next

    Jumps to the next iterator or item.

    Let's break that infinite loop we created earlier:


    loop do

    puts “R U A DUMB PROGRAMMER?”

    print “Enter YES to quit:”

    gets

    chomp

    break if $_ == “YES” # if the user types YES, break the loop

    end

    The program will repeatedly ask the user if they are dumb for all eternity until they admit that YES, they are a dumb programmer.

    Try, Try, Try Again

    They always tell you if at first you don't succeed, try, try again. Well that's how I got my restraining order (knocks on door: Is Mrs. Jolie home? Knocks on door: Is Mrs. Jolie home? Kicks and screams as cops drag him away). Unlike the police and their stupid anti-stalking laws, Ruby allows you to keep trying by using the Retry statement. It forces a loop or iterator to completely start over.


    Loop do

    puts “Can you spell orange?:”

    gets

    chomp

    retry if $_ != “orange” # remember != means does not equal

    end

    The above sample asks the user to spell the word orange. If they misspell it, the retry statement forces the loop to start again until they get it right.

    More Ruby-on-Rails Articles
    More By James Payne


       · The series on Ruby continues! In this episode we discuss loops, including the While,...
     

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