Ruby Exceptions

Watch out! This tutorial is over 3 years old. Please keep this in mind as some code snippets provided may no longer work or need modification to work on current systems.

Ruby uses exceptions. Instead of try, the block is called begin, and instead of catch there is rescue. The else is executed when no exception occurs.

Continue reading

Ruby Methods (Part 2)

Watch out! This tutorial is over 7 years old. Please keep this in mind as some code snippets provided may no longer work or need modification to work on current systems.

Here’s another use of methods. Notice that when we send an array as a parameter, changes in the function do update the value to the caller.

Continue reading

Ruby Methods (Part 1)

Watch out! This tutorial is over 7 years old. Please keep this in mind as some code snippets provided may no longer work or need modification to work on current systems.

Functions (and later methods) are defined with def. Parameters are given, without types of course. Default values may be provided. Note that when we send an integer, the method does not change the value sent.

Continue reading

Ruby Iterators

Watch out! This tutorial is over 7 years old. Please keep this in mind as some code snippets provided may no longer work or need modification to work on current systems.

Ruby iterators are methods which take and run a block of code. The block can be delimited by curly braces or by the keywords do and end. The brackets have higher precedence, and variables declared in them are destroyed when the bracked code exits.

Continue reading

Ruby Case Expression

Watch out! This tutorial is over 7 years old. Please keep this in mind as some code snippets provided may no longer work or need modification to work on current systems.

The ruby case statement is similar to the C/C++/Java switch, but more directly related to the similar (and superior) structures from Pascal and Ada. First, it assumes that each case ends where the next one starts, without needing a break to terminate a case. Secondly, each case can be expressed rather generally, with a single value, a range of value, or a list containing some of each.

Continue reading

Ruby For Loop

Watch out! This tutorial is over 7 years old. Please keep this in mind as some code snippets provided may no longer work or need modification to work on current systems.

The Ruby for loop is similar to the Python for or the perl foreach. Its basic operation is iterate over the contents of some collection. This may be an actual array or a range expression. A range expression is two numbers, surrounded by parens, and separated by either two dots or three. The three-dot form omits the last number from the range, while the two-dot version includes it. The three-dots version can be quite useful for creating a range of legal subscripts since the array size is not a legal subscript.

Continue reading

More Conditional Logic in Ruby!

Watch out! This tutorial is over 7 years old. Please keep this in mind as some code snippets provided may no longer work or need modification to work on current systems.

Following from the previous tutorial on Conditional Logic in Ruby, look at this example:

# Let the user guess.
print "Enter heads or tails? "
hort = gets.chomp
unless hort == 'heads' || hort == 'tails' 
    print "I _said_ heads or tails.  Can't you read?\n"
    exit(1)
end

# Now toss the coin.
toss = if rand(2) == 1 then
    "heads"
else
    "tails"
end

# Report.
print "Toss was ", toss, ".\n"
print "You Win!\n" if hort == toss

Some more amazing if lore:

  1. The unless keyword is like if, but uses the opposite sense of the test: The code is run if the test is false.
  2. As with many things in Rubyif may look like a statement, but it is actually an expression, with a return value.
  3. The if has a postfix form where the condition comes last. This works with unless as well.

Conditional Logic in Ruby

Watch out! This tutorial is over 7 years old. Please keep this in mind as some code snippets provided may no longer work or need modification to work on current systems.

This is the conventional use of the if in perl. Notice that there is no need for curly braces ({ and }). The body of the if ends with the appropriate keyword, endelse or elsif. The then word is generally optional, though you need it if you want to put start the body on the same line as the if, the way the last statement does.

Continue reading

Parallel Assignment in Ruby

Watch out! This tutorial is over 7 years old. Please keep this in mind as some code snippets provided may no longer work or need modification to work on current systems.

A parallel assignment sets several variables at once. All right sides are computed before any variable is assigned, so all right side values are computed with the old variable values. This is very convenient for swapping two values without using a temporary.

Continue reading