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 
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: 3 stars3 stars3 stars3 stars3 stars / 4
    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

    - Calculating Statistics with Active Record
    - Creating Graphs with Ruby
    - Callbacks and the Active Record
    - Validation and the Active Record
    - Arrays, Associations and the Active Record
    - Associations and Dependencies with Active Re...
    - Advanced Active Record: Enhancing Your Models
    - Load Balancing Databases with Rails
    - More Advanced Database Features and Rails
    - Handling Advanced Database Features with Rai...
    - Managing Database Files with Ruby on Rails
    - Databases and Ruby on Rails
    - Updating and Deleting with the Active Record
    - Rails Active Record and CRUD Functions
    - Working with a Database: Active Record







    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek