Home arrow Ruby-on-Rails arrow Page 2 - 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 - 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).


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