Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
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.

The File class also has a number of methods like exists and size which allow you to check a file status even without opening it

[link] Iterating through a file, and file output.

[link] 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:

[link] 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 articles
R106 - Input and Output in Ruby
  [4678] Expect with Ruby - a training example to get you started - (2016-05-18)
  [4499] Significant work - beyond helloworld in Ruby - (2015-05-27)
  [3429] Searching through all the files in or below a directory - Ruby, Tcl, Perl - (2011-09-09)
  [2974] Formatting your output - options available in Ruby - (2010-09-29)
  [2893] Exclamation marks and question marks on ruby method names - (2010-07-28)
  [2621] Ruby collections and strings - some new examples - (2010-02-03)
  [2614] Neatly formatting results into a table - (2010-02-01)
  [1887] Ruby Programming Course - Saturday and Sunday - (2008-11-16)
  [1587] Some Ruby programming examples from our course - (2008-03-21)


Back to
pre-Inaugural briefing - Melksham Community Area Partnership
Previous and next
or
Horse's mouth home
Forward to
Collection objects (array and hash) in Ruby
Some other Articles
Can you learn to program in 4 days?
Regular Expressions in Ruby
Object Orientation in Ruby - intermediate examples
Collection objects (array and hash) in Ruby
Opening and reading files - the ruby fundamentals
pre-Inaugural briefing - Melksham Community Area Partnership
Wiltshire Community Area Partnerships
Learning to program in Ruby - examples of the programming basics
New to programming? It is natural (but needless) for you to be nervous
Great 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 http://www.wellho.net/horse/ - 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).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2019: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/2290_Ope ... ntals.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat