#RubyTuesday

Watch out! This tutorial is over 1 year 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.

Watch out! This tutorial is over 1 year 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.

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

The rubycase statement is similar to the C/C++/Javaswitch, 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.

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

The Rubyfor loop is similar to the Pythonfor or the perlforeach. 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.

Watch out! This tutorial is over 1 year 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, end, else 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.

Watch out! This tutorial is over 2 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 built-in gets function simply reads a line and returns it as a string. The chomp method from class String trims the line terminator. The prompts are generated with ordinary prints lacking a final newline.

Ruby

1

2

3

4

5

6

7

8

9

10

11

12

# Get the parts of speech

print"Please enter a past-tense verb: "

verb=gets.chomp

print"Please enter a noun: "

noun=gets.chomp

print"Please enter a proper noun: "

prop_noun=gets.chomp

print"Please enter a an adverb: "

adv=gets.chomp

# Make the sentence.

print"#{prop_noun} got a #{noun} and\n#{verb} #{adv} around the block.\n"

Similarly, the to_f method of class String converts the input string to a floating-point number.

Watch out! This tutorial is over 2 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 generally uses line breaks instead of semicolons to separate statements. Lines can be continued by using a \ at the end, but this is rarely needed. If the line ends with pretty much anything that suggests there should be more, Ruby will continue on to the next line. This includes such things as ending with an operator, or inside parentheses or something else which needs to be closed.