Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
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 the gets
method on it, you'll get the next line back. Once you hit the end of the file, you'll get a nil
result which you can check for, and you're then supposed to close the file.
Once you've hit the end of the file, further calls to gets
will simply return nil
again; if you want to re-read the data, you'll have to open the file again, or use a rewind
method. Of course, you could read the whole file into an array with readlines
and then iterate through it a lot of times without going back to the disc, and that's going to be much quicker unless the file is huge.
class also has a number of methods like exists
which allow you to check a file status even without opening it
Iterating through a file, and file output.
Test file to see if it exists prior to opening.
You can also open another process and handle that as it it were a file:
Reading from another process
Examples written yesterday during our Learning to program in Ruby Course
. Similar principles apply across many of the langauges we teach with just the deatil and syntax varying, and we can also run "Learning to program in ..." courses in Python, Lua, PHP, .... (written 2009-07-16)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articlesR106 - Input and Output in Ruby 
Expect with Ruby - a training example to get you started - (2016-05-18) 
Significant work - beyond helloworld in Ruby - (2015-05-27) 
Searching through all the files in or below a directory - Ruby, Tcl, Perl - (2011-09-09) 
Formatting your output - options available in Ruby - (2010-09-29) 
Exclamation marks and question marks on ruby method names - (2010-07-28) 
Ruby collections and strings - some new examples - (2010-02-03) 
Neatly formatting results into a table - (2010-02-01) 
Ruby Programming Course - Saturday and Sunday - (2008-11-16) 
Some Ruby programming examples from our course - (2008-03-21)
Some other Articles
Can you learn to program in 4 days?Regular Expressions in RubyObject Orientation in Ruby - intermediate examplesCollection objects (array and hash) in RubyOpening and reading files - the ruby fundamentalspre-Inaugural briefing - Melksham Community Area PartnershipWiltshire Community Area PartnershipsLearning to program in Ruby - examples of the programming basicsNew to programming? It is natural (but needless) for you to be nervousGreat new diagrams for our notes ... Python releases
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page
This is a page archived from The Horse's Mouth at
the diary and writings of Graham Ellis.
Every attempt was made to provide current information at the time the
page was written, but things do move forward in our business - new software
releases, price changes, new techniques. Please check back via
our main site for current courses,
prices, versions, etc - any mention of a price in "The Horse's Mouth"
cannot be taken as an offer to supply at that price.
Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).