Ruby - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Ruby File io

Ruby File io

shape Introduction

This chapter demonstrates about the Ruby File io which have complete set of I/o methods derived from IO class and following are the concepts covered in this chapter.
  • About File
  • Creating and Opening a File
  • Deleting a File

About File

shape Description

A File defined as the digital information placed in durable storage space. Files are different from others files can exist before the program and after the program a program runs files can be shared and validated to all formats which is the common with one another. If user working with word processing application then user need to know creating files and which involves moving to the File menu, choosing another File or using any other combination of shortcuts to complete the task which have the several functions. puts Statement puts statement is used display the program and the value should be stored in variable by which user will get a new line at the end of present line which writes, the code below demonstrates the puts statement as shown below. [ruby] val1 = "This is variable one" val2 = "This is variable two" puts val1 puts val2 [/ruby] Result By running the above code in command prompt, the output can be obtained as shown in the image below. gets Statement gets statement will call STDIN and get any input from the user screens and after the value entered and stored in a variable val and those are printed by using the STDOUT the code is as shown below. [ruby] puts "Enter a value :" val = gets puts val [/ruby] Result By running the above code in command prompt, the output can be obtained as shown in the image below.

Creating and Opening a File

shape Description

User can create a File in Ruby with File.new method which is used reading and writing or both operations based on the string and user can also close the method by using the File.close method. The snippet below demonstrates the creating new file as shown below. [ruby] aFile = File.new("filename", "mode") # ... process the file aFile.close [/ruby] User can open the existing file by using File.open. In which it will create new file object and assign to a file the only difference between File.open and File.new is the File.open is associated with block but which is not possible with File.new. The code below demonstrates the File.open as shown. [ruby] File.open("filename", "mode") do |aFile| # ... process the file end [/ruby] In order to open the file which have some functions those are listed below. The code below demonstrates the sysread method as shown below. [ruby] aFile = File.new("input.txt", "r") if aFile content = aFile.sysread(20) puts content else puts "Unable to open file!" end [/ruby] Result By running the above code in command prompt, the output can be obtained as shown in the image below. The code below demonstrates the syswrite method as shown below. [ruby] aFile = File.new("input.txt", "r+") if aFile aFile.syswrite("ABCDEF") else puts "Unable to open file!" end [/ruby] Result By running the above code in command prompt, the output can be obtained as shown in the image below.

Deleting a File

shape Description

In a Ruby if user want to delete an unwanted file which not neededby giving the reference or name to a required file using File.delete. The code below demonstrates to delete an existing file as shown below. [ruby] # Delete file test1.txt File.delete("test1.txt") [/ruby] In Ruby File name can be renamed by using File.rename, the code below demonstrates renaming a file. [ruby] # Rename a file from test1.txt to test2.txt File.rename( "test1.txt", "test2.txt" ) [/ruby]

Summary

shape Key Points

  • In Ruby user can rename the file with out any effecting the data.
  • File is an digital information in durable storage.
  • If user work with word processing then user need to work with the files.