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||updated|
|4499||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 ...
|3429||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 ...
|2974||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 ...
|2893||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 ...
|2621||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 ...
|2614||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 ...
|2290||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 ...
|1887||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 ...
|1587||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
|afid.rb|| All files in a directory|
|amaze1|| Find and count matching lines in file|
|courseprice|| Checking if a file exists and writing to a file|
|d3_1|| iterating through a file, file output|
|d3_2|| Testing files|
|d3_3|| Reading from another process|
|ddd|| parse a directory|
|filemore|| Analysing a file / counting events|
|filestuff.rb|| File handling demonstration|
|finfo.rb|| File status checks|
|formprint|| Formatted printing|
|large|| Checking a whole directory tree|
|ndo|| Running another process - netstat example|
|sysact.rb|| Run a process from Ruby, stream output back|
|two|| chop and chop!|
|webreader.rb|| 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 http://www.training-notes.co.uk
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. Also
available is the Opentalk
Forum for discussion of technical questions.