For 2023 (and 2024 ...) - we are now fully retired from IT training. We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.
Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!
I am also active in many other area and still look after a lot of web sites - you can find an index ((here)) |
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 [1587] Some Ruby programming examples from our course - (2008-03-21) [1887] Ruby Programming Course - Saturday and Sunday - (2008-11-16) [2614] Neatly formatting results into a table - (2010-02-01) [2621] Ruby collections and strings - some new examples - (2010-02-03) [2893] Exclamation marks and question marks on ruby method names - (2010-07-28) [2974] Formatting your output - options available in Ruby - (2010-09-29) [3429] Searching through all the files in or below a directory - Ruby, Tcl, Perl - (2011-09-09) [4499] Significant work - beyond helloworld in Ruby - (2015-05-27) [4678] Expect with Ruby - a training example to get you started - (2016-05-18)
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
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).
|
|