March 2025
Intermediate to advanced
472 pages
12h 10m
English
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.
Ruby has all the usual control structures, such as if statements and while loops. Java, C, and Perl programmers may well get caught by the lack of braces around the bodies of these statements. Instead, Ruby uses the end keyword to signify the end of a body:
| | if count > 10 |
| | puts "Try again" |
| | elsif tries == 3 |
| | puts "You lose" |
| | else |
| | puts "Enter a number" |
| | end |
Similarly, while statements are terminated with end:
| | while weight < 100 and num_pallets <= 30 |
| | pallet = next_pallet() |
| | weight += pallet.weight |
| | num_pallets += 1 |
| | end |
Ruby also contains ...
Read now
Unlock full access