Thursday, January 20, 2011

Web Application Testing in Ruby: While Loop

Web Application Testing in Ruby: Web Application Testing in Ruby: Ruby scripting Basic

while Loops
All the loops previously discussed looped through either an array or a set of numbers. Sometimes you need a more generic loop. That's when you use a while loop:
#!/usr/bin/ruby
ss = 4
while ss > 0
puts ss
ss -= 1
end
puts "======================"
while ss < 5
puts ss
ss += 1
break if ss > 2
end
puts "======================"
ss = 5
while ss > 0
puts ss
ss -= 2
if ss == 1
ss += 5
end
end
[slitt@mydesk slitt]$ ./loop.rb
4
3
2
1
======================
0
1
2
======================
5
3
6
4
2
[slitt@mydesk slitt]$

The first while loop iterated from 4 down to 1, quitting when ss became 0 and hit the while condition. The second loop was intended to iterate up to 4 and quit when 5 was encountered, but a break statement inside the loop caused it to terminate after printing 2 and then incrementing to 3. This demonstrates the break statement.

The third loop was intended to loop from 5 down to 1, quitting after printing 1 and then decrementing. However, the statement in the body of the loop added 5 when it reached 1, pushing it back up to 6, so it had to count down again. On the second countdown, the numbers were even, so it didn't trigger the if statement. This shows that unlike Pascal, it's OK to tamper with the loop variable inside the loop.
Branching
Looping is one type of flow control in pure procedural languages. The other is branching. The following program implements an array called democrats and another called republicans . Depending on the command line argument, the program prints either the democratic presidents since 1974, the republican presidents since 1974, or an appropriate error message.
#!/usr/bin/ruby
democrats = ["Carter", "Clinton"]
republicans = ["Ford", "Reagan", "Bush1", "Bush2"]
party = ARGV[0]
if party == nil
print "Argument must be \"democrats\" or \"republicans\"\n"
elsif party == "democrats"
democrats.each { |i| print i, " "}
print "\n"
elsif party == "republicans"
republicans.each { |i| print i, " "}
print "\n"
else
print "All presidents since 1976 were either Democrats or Republicans\n"
end

Note the if, elsif, else and end keywords, and how they delineate the branching. Note also the democrats.each syntax, which is a very shorthand way of iterating through an array, assuming what you want to do to each element can be stated succinctly.

One last note. The error handling in the preceding would be much better handled by exceptions, but they haven't been covered yet.

Like Perl, the if keyword can follow the action instead of preceding it:
#!/usr/bin/ruby
democrats = ["Carter", "Clinton"]
republicans = ["Ford", "Reagan", "Bush1", "Bush2"]
party = ARGV[0]
if party != nil
democrats.each { |i| print i, " "} if party == "democrats"
republicans.each { |i| print i, " "} if party == "republicans"
print "All presidents since 1976 were either Democrats or Republicans\n"\
if (party != "democrats" && party != "republicans")
end

The preceding is a very contrived program to showcase using the if keyword after the action. Note the following:
1. The if keyword must be on the same line as the action
2. Only a single action can precede the if keyword. Multiple actions separated by semicolons will do quite unexpected things.

No comments:

Post a Comment