Exercises, examples and other material relating to training module R106. This topic is presented on public courses Learning to program in Ruby
, Ruby Programming
User and file input and output -
and also reading from other streams
such as connections to other processes
and other (remote) web services. Not to
forget file system checking and manipulation
|Articles and tips on this subject
|Expect with Ruby - a training example to get you started
The Expect library, originally written for use wth Tcl, is also available in other languages and today I wrote an example to introduce how it's used in Ruby. Please note this is a spike solution - not going to fail (if it does) in a pretty way.
The principle of expect is that you spawn a process - ...
|Significant work - beyond helloworld in Ruby
A little program can do a LOT of work! Scenario - I have a web server log file of some 50 Mbytes (the data from one particular day on our server) and the secon field in each line tells me which of our hosted web sites was being visited. The question I was asked - "how many hits on each host?". Solution ...
|Searching through all the files in or below a directory - Ruby, Tcl, Perl
Many of our customers want to learn how to traverse all the files in a directory, or perhaps even all the files in or below a directory. I quite often write a demonstration program during our courses which looks though part of a file system tree for files over a certain size, or for the largest (so ...
|Formatting your output - options available in Ruby
Almost every program I've ever written (and almost every program you'll ever write) is required to output a mixture of constant character strings and results - formatting the output into a human readable form, or adding in separators between values in data files which the next program in a chain will ...
|Exclamation marks and question marks on ruby method names
In ruby, you'll find some function/method names that are just a series of letters (a rather conventional sort of name for a function or a variable), and others that have a ! or ? on the end. What's the significance of these?
• A function with a ! on the end usually alters the incoming object ...
|Ruby collections and strings - some new examples
From yesterday's Ruby course - a whole set of new examples on Ruby collections - arrays (which are ordered lists) and hashes (which are unordered). In other words, you look somethng up in an array by its position number, whereas you look something up in a hash based on its unique key, and the way it's ...
|Neatly formatting results into a table
Would you like to produce a neat table of results, with the columns just wide enough to take the data? That sounds straightforward, but you can't output anything until you've looked through all the lines to see how long each of them will be.
I was talking about this algorithm today, and this evening ...
|Opening and reading files - the ruby fundamentals
Once you've noted that File.new doesn't create a new file, but rather (default) opens an existing one for read ... you'll find Ruby's file handling interface easy to use. The new methof returns a file handle object - a sort of buffer that sits between the file and your program - and each time you run ...
|Ruby Programming Course - Saturday and Sunday
We're so busy during the week at the moment that I'm running an extra Ruby Course this weekend. The main use of Ruby is within "Ruby on Rails", but our course concentrates on the language itself and so it's suitable for people who are using RSpec, cucumber and Watir as well as Rails.
Here are some ...
|Some Ruby programming examples from our course
I was giving a Public Ruby Course to a small group at the end of last week ... and having a small group gave me the opportunity to write some demonstrations in front of them. I have now tidied these up and have pleasure in presenting to more Ruby demonstrations:
Ruby's BEGIN block
The compact method ...
Examples from our training material
| All files in a directory
| Find and count matching lines in file
| Checking if a file exists and writing to a file
| iterating through a file, file output
| Testing files
| Reading from another process
| parse a directory
| Analysing a file / counting events
| File handling demonstration
| File status checks
| Formatted printing
| Checking a whole directory tree
| Running another process - netstat example
| Run a process from Ruby, stream output back
| chop and chop!
| Read a page given its url
Some modules are available for download
as a sample of our material or under an Open Training Notes License
for free download from [here]
Topics covered in this module
Opening and closing files.
Reading and writing from files.
If you are looking for a complete course and not just a information on a single subject, visit our Listing and schedule
Well House Consultants specialise in training courses in
. We run
throughout the UK (and beyond for longer courses), and
at our training centre in Melksham, Wiltshire, England.
It's surprisingly cost effective to come on our public courses -
even if you live in a different
country or continent to us
We have a technical library of over 700 books on the subjects on which we teach.
These books are available for reference at our training centre.