Ruby/Flow Control

From Wikiversity
Jump to navigation Jump to search

Overview[edit | edit source]

By the end of this lesson you should be able to:

  • Use dot-times-loop.
  • Use while and until loops.
  • Use if.


Loops[edit | edit source]

First of all, you should know what a loop is. Therefore, I will tell you:

A loop is the thing you use, when you want to do something again and again. For example, let's say that you wanted to greet five persons: Peter, Jones, Bill, Sophie and Anne. In ruby you would write:

   # puts is same as print, but appends a newline.
   puts "Hello, Peter"
   puts "Hello, Jones"
   puts "Hello, Bill"
   puts "Hallo, Sophie"       # I've typed 'Hallo' not 'Hello'
   puts "Hello, Anne"

But, whith a loop you could write:

   ["Peter", "Jones", "Bill", "Sophie", "Anne"].each do |name|
      puts "Hello, #{name}"
   end

Which gives the same, but without any typos--much smarter. So, lets learn something about loops:

The dot-times loop[edit | edit source]

I don't really know what you call this one, but dot-times is a good name, isn't? Anyways, this is used like:

   5.times do
     print "Again and "
   end

This will print:

   Again and Again and Again and Again and Again and

Not perfect but still, the idea is there, right? So, now I am supposed to tell you how that works... and I will, in a moment, but first let's get something clear. The do and ends makes a block, a block is some code that is sticked together. The dot means that we want to do something with the number 5. And the times is what we want to do with the number. Everything that is inside the do and the end is a block. And what is in the block will be executed each time the loop runs.

The while loop[edit | edit source]

Ruby can also do while-loops. While loops are found in many other programming languages. They are a bit of an old programming tradition. An example:

    count = 0
    while count < 5
      print 'The count is now ', count, '. '
      count = count + 1
    end
    Result: The count is now 0. The count is now 1. The count is now 2. The count is now 3. The count is now 4.

The until loop[edit | edit source]

The until loop is a variation on the while loop.

    count = 0
    until count == 5
      print 'The count is now ', count, '. '
      count = count + 1
    end
    
    Result: same as above.

Pay attention to the double equal signs. In Ruby and many other programming languages a == 5 means Compare a to 5, and a = 5 means Make a point to a value of 5.

If[edit | edit source]

Ruby call also execute code only if certain conditions are met, this is the 'if' function. For example:

    if(count == 5)
      print 'The count is equal to five'
    end

What this means is really "if count is equal to five the execute this code." this will also work with other mathematical operators.

else[edit | edit source]

'else' is another part of the 'if' function this will execute if the 'if' condition(s) are not met. For example:

    if(count == 5)
      print 'The count is equal to five'
    else
      print 'The count is not equal to five'
    end

elsif[edit | edit source]

'elsif' is a variation on the 'else' function it is really a contraction of the 'else' and 'if' functions

    if(count == 5)
      print 'The count is equal to five'
    else
      if(count < 5)
        print 'The count is less than five'
      end
    end

The above code can be modified to do the same thing with the 'elsif' function

    if(count == 5)
      print 'The count is equal to five'
    elsif(count < 5)
      print 'The count is less than five'
    end

This code will do the same as the first piece but it is shorter and tidier than the first peice