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

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

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.

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

print "Triangle height: "
h = gets.to_f;
print "Triangle width: "
w = gets.to_f;
area = 0.5*h*w
print "Triangle height ", h, " width ", w, " has area ", area, "\n"

Line Breaking with 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.

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.

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

Strings in Ruby

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 has many ways of making strings, which are generally variations of the many ways Perl has of making strings. Double quotes allow values to be interpolated into the string, while single quotes do not. Most escapes are treated literally in single quotes, including the fact that \n is treated as two characters, a backward slash followed by an n.

Continue reading

Ruby Expressions

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.

This is the first in a series for beginners on the Ruby Programming Language. Note that Ruby is a programming language. Ruby on Rails (“RoR”) is a web-application framework that is implemented in Ruby. Do not confuse the two. We will not covering Ruby on Rails in these tutorials.

These tutorials will be gather together under the tag #RubyTuesday, as we will be posting one each week from today to the end of term.  Ready? Here we go

Continue reading