Logics
Let’s dive into Ruby's control structures, iterators, and exceptions.
We'll cover the following...
We'll cover the following...
Method calls are statements. Ruby also provides a number of ways to make decisions that affect the repetition and order in which methods are invoked.
Control structures
Ruby has all the usual control structures, such as if statements and while loops. Java, C, and Perl programmers may get caught by the lack of braces around the bodies of these statements though, since Ruby uses the end keyword to signify the end of a body:
Ruby
count = 9 # You can play around with thistries = 3 # You can play around with thisif count > 10puts "Try again"elsif tries == 3puts "You lose"elseputs "Enter a number"end
Similarly, while statements are terminated with end:
Ruby
weight = 2 # You can play around with these valuesnum_pallets = 10 # You can play around with these valueswhile weight < 100 and num_pallets <= 30weight += 1num_pallets += 1endputs num_pallets
Ruby also contains variants of these statements. The unless statement is like if except that it checks for the condition to not be true. Similarly, until is like while except that the loop ...