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 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

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

File I/O 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 version opens a file for writing, and writes its output to a file selected by the user. The second argument to open is the same as the second argument to a plain C fopen, except it will default to reading.

The object returned from open is class File.

Continue reading

Ruby Hashes

Watch out! This tutorial is over 8 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 hashes are similar to maps or dictionaries in other languages. They are essentially arrays whose subscripts are not limited to integer values. You can create them with the curly-bracked list notation shown below, but you can also assign to a subscript expression to add members. It’s not at all unreasonable to create a hash with empty brackets, then add members using subscripting.

Continue reading