Home arrow Ruby-on-Rails arrow Page 3 - Ruby Loops
RUBY-ON-RAILS

Ruby Loops


After last week, I know you've been eagerly waiting for this, the fifth article in my Ruby on Rails series. This one will give you an in-depth look at Loops, Methods, and Blocks. Well maybe Blocks. You'll have to see. So without a bunch of unnecessary yammering, let's get to it.

Author Info:
By: James Payne
Rating: 5 stars5 stars5 stars5 stars5 stars / 16
October 22, 2007
TABLE OF CONTENTS:
  1. · Ruby Loops
  2. · The While Loop
  3. · Stop that Loop!
  4. · Reach Out and Touch Someone
  5. · Scope

print this article
SEARCH DEVARTICLES

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.


blog comments powered by Disqus
RUBY-ON-RAILS ARTICLES

- Ruby-on-Rails Faces Second Security Flaw in ...
- Ruby 2.0 Prepped for February 2013 Release
- Why LinkedIn Switched from Ruby on Rails
- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials