Training, Open Source computer languages

PerlPHPPythonMySQLhttpd / TomcatTclRubyJavaC and C++LinuxCSS

Search our site for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Breaking a loop - Ruby and other languages
When you're in a loop there are occasions you want to say get me our of this loop NOW, or "I'm done with the current iteration. And those are the 'classic' break and continue statements from C, C++ and Java. Languages like Perl changed break to last and continue to next ... and added a redo that asks for the current iteration to be rurun.

With me so far?

Now in Ruby ... you have break, you have next, you have redo and you also have retry ... which reenters the top of the loop. Wow - perhaps I had better provide an example?

for i in 1..5
print "How many do you need on day #{i}? "
nneed = gets.chomp.to_i

# next - don't order nothing; move on to next case
next if nneed == 0

# retry - whole thing to be redone on a 999 code
retry if [112,911,999].include?(nneed)

# redo - more than 10 ordered - must be a mistake
if nneed > 10 then print "Too many!\n"; redo; end

# break - code "-1" entered to exit on a short week
break if nneed == -1

print "We'll get #{nneed} on order for day #{i}\n"
end


By the way - if you've not seen much Ruby before, have a look through that example and taste a few of the constructs. It's really neat, and I expect I'll be writing a lot more about it in the future. ;-)

New technical articles on Ruby:
How classes are defined and used
Ruby's Control statements
String functions in Ruby
Ruby Regular Expressions
Modules, Mixins and Comparators
(written 2006-12-03 18:30:59)

 
Associated topics are indexed under
R104 - Ruby - Control Structures
H104 - PHP - Control Statements
P206 - Perl - More Loops and Conditionals
C203 - C and C based languages - Conditionals and Loops

Back to
Products that our customers want more of
Previous and next
or
Horse's mouth home
Forward to
George Hotel and Well House Manor, Melksham

Some other Articles
CSL, KISS and RTFM
KISS - one action per statement please - Perl
Practical polymorphism in action
George Hotel and Well House Manor, Melksham
Breaking a loop - Ruby and other languages
Products that our customers want more of
1st, 2nd, 3rd revisited in Ruby
It's the 1st, not the 1nd 1rd or 1th.
Plain Ole nice pictures
Improving the historic town of Melksham
1638 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

© WELL HOUSE CONSULTANTS LTD., 2008: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho