Ruby-on-Rails
  Home arrow Ruby-on-Rails arrow Page 2 - 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 - The While Loop


    (Page 2 of 5 )

    The While Loop tells the program to continue looping while a certain criteria you specify is true.


    While($_ != “Stop”)

    puts “Input the right code or this computer will explode:”

    gets

    chomp

    end

    If you type anything but Stop, the program will repeat itself. The only way for the program to cease exploding is to type Stop. Remember, the != operator means does not equal.

    The Until Loop

    Everything has to have its arch-nemesis, its polar opposite, and the While Loop is no different. Behold: The Mighty Until Loop! It works exactly like the While Loop, only in the complete opposite fashion (I'll let that settle in).

    Basically, the Until Loop tells the program to continue looping until a certain criteria is met.



    Until($_ ==”Stop”)

    puts “This computer will explode if you enter the wrong code:”

    gets

    chomp

    end

    As with the While Loop, the program will loop until you type the word Stop. So what's the difference? There is none really, just a different thought process. Which you use is totally a matter of taste.

    Iterators

    Another method of creating loops is the iterator. We will touch on it briefly here.


    10.times do

    puts “I am the king!”

    end

    The above is an example of the Times Iterator, which lets you iterate code x amount of times (in the above instance, 10 times). If you execute the above code it would return the following results:

      “I am the king!”

      “I am the king!”

      “I am the king!”

      “I am the king!”

      “I am the king!”

      “I am the king!”

      “I am the king!”

      “I am the king!”

      “I am the king!”

      “I am the king!”

    If you wish to create an infamous infinite loop, you could use the Loop Iterator:


    loop do

    puts “I R A DUMB PROGRAMMER!”

    end

    This would loop the phrase over and over again forever (or until you shut off the computer).

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