Covered in this Episode:
- Defining I/O
Hello Everybody and welcome to Programming With Ruby Episode 13, Basic
input output. As always, I’m Tyler and this video is brought to you by
In this episode I will be defining what exactly input/output is, You
will also learn how to work with files and directories in Ruby
Lets get started!
So what exactly is I/O or input/output. Basically Input is any data that
you receive from the user, and output is what data you give to the
user, usually based on what input the user gave you.
In this episode you will be seeing I/O as working with files and
directories. You have already worked with I/O previously when we
talked about getting strings from the user (gets) and printing string
to the screen (puts)
There are a couple of ways to start working with files. The first is
by using File.new:
my_file = File.new('file', 'r') my_file.close
File.new opens the file (the first parameter), for the specified mode
(the second parameter)
The file name you use can be either just the relative file name
(notes.txt) or the full path to the file (C:\Documents and
Settings\Tyler\My Documents\notes.txt). Just remember that if you use
only the file name, that file must be in the same folder your program
is located in!
The mode defines how you are going to use the file you’ve opened. In
the example ‘r’ is used. Which stands for read, meaning we are only
going to read from the file.
‘w’ is write meaning we are writing to the file specified, be careful
when using write mode, as the whole file is cleared when it is opened.
‘a’ is append, which is similar to write, except instead of clearing
the whole file, it appends all written data to the end.
All the different modes can have ‘+’ at the end of them, which opens
up both read and write functionality. (ex: ‘w+’)
Now you know how to open a file, how exactly do yo go about writing to
it? By simply using puts and print, like you did when outputting data
to the screen, except they output data to the file!
my_file = File.new('example.txt', 'w') my_file.puts "Hello World!" my_file.close
If you want to read from a file you can use File.read:
my_file = File.new('example.txt', 'r') puts my_file.read my_file.close
You can also pass in how many characters you want to read as a parameter.
If you would rather have the lines of a file in an array, you can use
If you are unsure a file exists you can simply use File.exists?:
Now it is important to note that every time I am done using a file I
call the .close method, why do I do this? Because if you don’t close
your access on the file, it can become unusable by other
programs. Also, your computers operating system might not write the
changes you’ve made to the file, until your program has released its
hold on it.
As an alternative to using file.close every single time, we can use
File.open, instead of File.new. File.open lets us use the file in a
code block, at the end of the code block, the file is closed
File.open('hello.txt', 'w') do |file| file.puts "Hello World!" end
There is a whole lot more you can do with files in Ruby to learn more
go to http://ruby-doc.org/core-1.8.7/index.html and find the File and IO class
You can work with directories (also known as folders) in Ruby using
Dir. You can create directories with Dir.mkdir:
You can get an array of all the files and directories that are in the
current directory with Dir.entries:
Keep in mind that . is your current directory, and .. is the directory
one level above the current one. Also, like files you can specify the
relative path or the full path.
There is also another method similar to Dir.entries, Dir.foreach. They
are essentially the same, except foreach allows you to iterate over
each item in a code block.
Dir.foreach('.') do |entry| puts entry end
Like Files, there is a lot of material I couldn’t cover to learn more
go to http://ruby-doc.org/core-1.8.7/index.html and find the Dir class
This brings us to the end of the episode.
If you like these videos please donate.
And if you have any Questions, Comments or Suggestions, you can leave
a comment in the comment box below, or email me at
Thank you very much for watching, goodbye.Programming With Ruby Episode 12, DocumentationProgramming With Ruby Episode 14, YAML