TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to RUBY

Ruby File Handling: File and IO Classes

Handle files: open a file with File.open and sequentially read in each line until eof.
Files. The nature of data involves persistence. In files, data persists longer than in memory. It lasts through restarts and can be transferred. It is durable.
In Ruby, we have methods like readline and readlines() that can incrementally process files. This is usually best for performance.
Open. This example opens a file with File.open. It reads in each line of the file. It uses an until-loop to sequentially access each line, until the end of the file (eof).

Path: The file opened should be stored in the same directory as the program. The leading slash indicates this.

Tip: Reading in files line-by-line is often more efficient for long files than reading them all at once. It uses less memory.

Here: In this example, the data.readline() call returns the line read. It includes the trailing newline, if one exists.

Print: When we use the print method, the trailing newline is printed to the output.

Contents of data.txt Line 1 Line 2 And line 3 Ruby program that uses File.open # Open data file with open. data = File.open("/data.txt") # Until end-of-file. until data.eof() # Read line. line = data.readline() # Print line. print line end Output Line 1 Line 2 And line 3
Exists. A file that does not exist cannot be read. In clearer terms, we must first test a file for existence. In Ruby we use the "File.exists?" method for this.

True, false: The "exists?" predicate method returns true or false. We can test it directly in an if-statement.

Here: This program will likely print "No" on your system. If you place a "Codex.txt" file in the root directory, it prints "Yes".

Ruby program that uses File.exists # See if this file exists. if File.exists?("/Codex.txt") puts "Yes" else puts "No" end Output Yes
Size. How can you get the size, in bytes, of a file? A simple approach is to use File.stat and access its size property. This returns the number of bytes in the file.

Info: For this example, make sure to change the path to a file that exists. An exception is encountered if the file does not exist.

Ruby program that gets file size # Get size of this file. s = File.stat("/Codex/p") bytes = s.size # Display the file size. puts bytes Output 106252
Readlines. It is easy to place an entire text file into an array of lines. This allows us to loop over and process the array, avoiding further disk reads.

Here: We invoke IO.readlines, which receives a file path argument. Often we can use relative paths.

Next: We read a file called "file.txt" in my "C:\Codex\" folder. For your system, please change the path to one that exists.

Ruby program that uses readlines lines = IO.readlines("/Codex/file.txt") # Display line count. puts lines.length # Loop over all lines in the array. lines.each do |line| puts line end Output 3 Line one Line two Line three
IO.foreach. This method iterates over the lines in a file. We pass it the file location. We can use IO.foreach in a do-loop with an iteration variable or with an iterator block.

Note: The first two lines have only eight characters, but have lengths of 9. The ending whitespace character "\n" is being counted.

Ruby program that uses IO.foreach # Use IO.foreach with do. IO.foreach("/Codex/file.txt") do |line| puts line end # Display lengths of lines with block. IO.foreach("/Codex/file.txt") {|line| puts line.length} Output Line one Line two Line three 9 9 10
Newlines. When we read lines from files, the trailing newlines are included. We must use a method like chomp() to clean up our strings.

Here: We call "chomp!" on each string returned from readline. Many other string methods can be used.

String, Chomp
File contents: gems.txt ruby sapphire diamond emerald topaz Ruby program that uses chomp, readline data = File.open("/files/gems.txt") until data.eof() line = data.readline() # Chomp line to remove trailing newline. line.chomp! puts "[" << line << "]" end Output [ruby] [sapphire] [diamond] [emerald] [topaz]
Comma-separated values. Split can be paired with an iterator like IO.foreach. In this way we can handle CSV files. An example is available for the split() method.Split, File
Complete support. Ruby provides complete support for file handling. It accesses the file system with methods from the IO and File classes. IO is integrated into the language.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf