Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Mohamed Ayman
Mohamed Ayman

Posted on

     

File Input and Output in Ruby

As we know that Ruby one of the language for backend technology, so it must have a module to deal with file system in your Operating system.

Ruby I/O is a way to interact with your system. Data is sent in the form of bytes/characters. IO class is the basis for all input and output in Ruby. It may be duplexed, hence may use more than one native operating system stream.

IO has a subclass as File class which allows reading and writing files in Ruby. The two classes are closely associated. IO object represent readable/writable interactions to keyboards and screens.

Common modes in I/O port

As it is known in most of other languages that have file system module, they have modes when dealing with file.

like if you want to append, delete, add, or read and this modes in ruby when you dealing with files:

  • "r": read-only mode is the default mode starts at beginning of file.
  • "r+": read-write mode, starts at beginning of file.
  • "w": write-only mode, either creates a new file or truncates an existing file for writing.
  • "w+": read-write mode, either creates a new file or truncates an existing file for reading and writing.
  • "a": write-only mode, if file exists it will append the file otherwise a new file will be created for writing only.
  • "a+": read and write mode, if file exists it will append the file otherwise a new file will be created for writing and reading.- "r": read-only mode is the default mode starts at beginning of file.
  • "r+": read-write mode, starts at beginning of file.
  • "w": write-only mode, either creates a new file or truncates an existing file for writing.
  • "w+": read-write mode, either creates a new file or truncates an existing file for reading and writing.
  • "a": write-only mode, if file exists it will append the file otherwise a new file will be created for writing only.
  • "a+": read and write mode, if file exists it will append the file otherwise a new file will be created for writing and reading.

Ruby opening a file

A Ruby file can be created using different methods for reading, writing or both.

f=File.new("fileName.rb")
Enter fullscreen modeExit fullscreen mode

or

File.open("fileName.rb","mode")do|f|
Enter fullscreen modeExit fullscreen mode
  • The different between open and new methods that open can take block of code and determine a specific mode.

Ruby reading a file

There are three different methods to read a file.

To return a single line, following syntax is used.

whileline=getsputslineend
Enter fullscreen modeExit fullscreen mode

To return the whole file after the current position, following syntax is used.

f.read
Enter fullscreen modeExit fullscreen mode

To return file as an array of lines, following syntax is used.

f.readlines
Enter fullscreen modeExit fullscreen mode

Ruby writing a file

#!/usr/bin/rubyaFile=File.new("about.txt","r+")ifaFileaFile.syswrite("New content is written in this file.\n")end
Enter fullscreen modeExit fullscreen mode

Ruby renaming and deleting a file

Ruby files are renamed using rename method and deleted using delete mehtod.

To rename a file, following syntax is used.

File.rename("old.txt","new.txt")
Enter fullscreen modeExit fullscreen mode

To delete a file, following syntax is used.

File.delete("filename.txt")
Enter fullscreen modeExit fullscreen mode

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Joined

More fromMohamed Ayman

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp