- GUI
- Windows API tutorial
- Introduction to Windows API
- Windows API main functions
- System functions in Windows API
- Strings in Windows API
- Date & time in Windows API
- A window in Windows API
- First steps in UI
- Windows API menus
- Windows API dialogs
- Windows API controls I
- Windows API controls II
- Windows API controls III
- Advanced controls in Windows API
- Custom controls in Windows API
- The GDI in Windows API
- PyQt4 tutorial
- PyQt5 tutorial
- Qt4 tutorial
- Introduction to Qt4 toolkit
- Qt4 utility classes
- Strings in Qt4
- Date and time in Qt4
- Working with files and directories in Qt4
- First programs in Qt4
- Menus and toolbars in Qt4
- Layout management in Qt4
- Events and signals in Qt4
- Qt4 Widgets
- Qt4 Widgets II
- Painting in Qt4
- Custom widget in Qt4
- The Breakout game in Qt4
- Qt5 tutorial
- Introduction to Qt5 toolkit
- Strings in Qt5
- Date and time in Qt5
- Containers in Qt5
- Working with files and directories in Qt5
- First programs in Qt5
- Menus and toolbars in Qt5
- Layout management in Qt5
- Events and signals in Qt5
- Qt5 Widgets
- Qt5 Widgets II
- Painting in Qt5
- Custom widget in Qt5
- Snake in Qt5
- The Breakout game in Qt5
- PySide tutorial
- Tkinter tutorial
- Tcl/Tk tutorial
- Qt Quick tutorial
- Java Swing tutorial
- JavaFX tutorial
- Java SWT tutorial
- wxWidgets tutorial
- Introduction to wxWidgets
- wxWidgets helper classes
- First programs in wxWidgets
- Menus and toolbars in wxWidgets
- Layout management in wxWidgets
- Events in wxWidgets
- Dialogs in wxWidgets
- wxWidgets widgets
- wxWidgets widgets II
- Drag and Drop in wxWidgets
- Device Contexts in wxWidgets
- Custom widgets in wxWidgets
- The Tetris game in wxWidgets
- wxPython tutorial
- Introduction to wxPython
- First Steps
- Menus and toolbars
- Layout management in wxPython
- Events in wxPython
- wxPython dialogs
- Widgets
- Advanced widgets in wxPython
- Drag and drop in wxPython
- Internationalisation
- Application skeletons in wxPython
- The GDI
- Mapping modes
- Creating custom widgets
- Tips and Tricks
- wxPython Gripts
- The Tetris game in wxPython
- C# Winforms Mono tutorial
- Java Gnome tutorial
- Introduction to Java Gnome
- First steps in Java Gnome
- Layout management in Java Gnome
- Layout management II in Java Gnome
- Menus in Java Gnome
- Toolbars in Java Gnome
- Events in Java Gnome
- Widgets in Java Gnome
- Widgets II in Java Gnome
- Advanced widgets in Java Gnome
- Dialogs in Java Gnome
- Pango in Java Gnome
- Drawing with Cairo in Java Gnome
- Drawing with Cairo II
- Nibbles in Java Gnome
- QtJambi tutorial
- GTK+ tutorial
- Ruby GTK tutorial
- GTK# tutorial
- Visual Basic GTK# tutorial
- PyGTK tutorial
- Introduction to PyGTK
- First steps in PyGTK
- Layout management in PyGTK
- Menus in PyGTK
- Toolbars in PyGTK
- Signals & events in PyGTK
- Widgets in PyGTK
- Widgets II in PyGTK
- Advanced widgets in PyGTK
- Dialogs in PyGTK
- Pango
- Pango II
- Drawing with Cairo in PyGTK
- Drawing with Cairo II
- Snake game in PyGTK
- Custom widget in PyGTK
- PHP GTK tutorial
- C# Qyoto tutorial
- Ruby Qt tutorial
- Visual Basic Qyoto tutorial
- Mono IronPython Winforms tutorial
- Introduction
- First steps in IronPython Mono Winforms
- Layout management
- Menus and toolbars
- Basic Controls in Mono Winforms
- Basic Controls II in Mono Winforms
- Advanced Controls in Mono Winforms
- Dialogs
- Drag & drop in Mono Winforms
- Painting
- Painting II in IronPython Mono Winforms
- Snake in IronPython Mono Winforms
- The Tetris game in IronPython Mono Winforms
- FreeBASIC GTK tutorial
- Jython Swing tutorial
- JRuby Swing tutorial
- Visual Basic Winforms tutorial
- JavaScript GTK tutorial
- Ruby HTTPClient tutorial
- Ruby Faraday tutorial
- Ruby Net::HTTP tutorial
- Java 2D games tutorial
- Java 2D tutorial
- Cairo graphics tutorial
- PyCairo tutorial
- HTML5 canvas tutorial
- Python tutorial
- Python language
- Interactive Python
- Python lexical structure
- Python data types
- Strings in Python
- Python lists
- Python dictionaries
- Python operators
- Keywords in Python
- Functions in Python
- Files in Python
- Object-oriented programming in Python
- Modules
- Packages in Python
- Exceptions in Python
- Iterators and Generators
- Introspection in Python
- Ruby tutorial
- PHP tutorial
- Visual Basic tutorial
- Visual Basic
- Visual Basic lexical structure
- Basics
- Visual Basic data types
- Strings in Visual Basic
- Operators
- Flow control
- Visual Basic arrays
- Procedures & functions in Visual Basic
- Organizing code in Visual Basic
- Object-oriented programming
- Object-oriented programming II in Visual Basic
- Collections in Visual Basic
- Input & output
- Tcl tutorial
- C# tutorial
- Java tutorial
- AWK tutorial
- Jetty tutorial
- Tomcat Derby tutorial
- Jtwig tutorial
- Android tutorial
- Introduction to Android development
- First Android application
- Android Button widgets
- Android Intents
- Layout management in Android
- Android Spinner widget
- SeekBar widget
- Android ProgressBar widget
- Android ListView widget
- Android Pickers
- Android menus
- Dialogs
- Drawing in Android
- Java EE 5 tutorials
- Introduction
- Installing Java
- Installing NetBeans 6
- Java Application Servers
- Resin CGIServlet
- JavaServer Pages, (JSPs)
- Implicit objects in JSPs
- Shopping cart
- JSP & MySQL Database
- Java Servlets
- Sending email in a Servlet
- Creating a captcha in a Servlet
- DataSource & DriverManager
- Java Beans
- Custom JSP tags
- Object relational mapping with iBATIS
- Jsoup tutorial
- MySQL tutorial
- MySQL quick tutorial
- MySQL storage engines
- MySQL data types
- Creating, altering and dropping tables in MySQL
- MySQL expressions
- Inserting, updating, and deleting data in MySQL
- The SELECT statement in MySQL
- MySQL subqueries
- MySQL constraints
- Exporting and importing data in MySQL
- Joining tables in MySQL
- MySQL functions
- Views in MySQL
- Transactions in MySQL
- MySQL stored routines
- MySQL Python tutorial
- MySQL Perl tutorial
- MySQL C API programming tutorial
- MySQL Visual Basic tutorial
- MySQL PHP tutorial
- MySQL Java tutorial
- MySQL Ruby tutorial
- MySQL C# tutorial
- SQLite tutorial
- SQLite C tutorial
- SQLite PHP tutorial
- SQLite Python tutorial
- SQLite Perl tutorial
- SQLite Ruby tutorial
- SQLite C# tutorial
- SQLite Visual Basic tutorial
- PostgreSQL C tutorial
- PostgreSQL Python tutorial
- PostgreSQL Ruby tutorial
- PostgreSQL PHP tutorial
- PostgreSQL Java tutorial
- Apache Derby tutorial
- SQLAlchemy tutorial
- MongoDB PHP tutorial
- MongoDB Java tutorial
- MongoDB JavaScript tutorial
- MongoDB Ruby tutorial
- Spring JdbcTemplate tutorial
- JDBI tutorial
Input & output in Ruby
In this part of the Ruby tutorial, we will talk about input & output operations in Ruby. Input is any data that is read by the program, either from a keyboard, file or other programs. Output is data that is produced by the program. The output may go to the screen, to a file or to another program.
Input & output is a large topic. We bring forward some examples to give you a general idea of the subject. Several classes in Ruby have methods for doing input & output operations. For example Kernel
, IO
, Dir
or File
.
Writing to console
Ruby has several methods for printing output on the console. These methods are part of the Kernel
module. Methods of the Kernel
are available to all objects in Ruby.
#!/usr/bin/ruby print "Apple " print "Apple\n" puts "Orange" puts "Orange"
The print
and puts
methods produce textual output on the console. The difference between the two is that the latter adds a new line character.
print "Apple " print "Apple\n"
The print method prints two consecutive "Apple" strings to the terminal. If we want to create a new line, we must explicitly include a newline character. The newline character is \n
. Behind the scenes, the print
method actually calls the to_s
method of the object being printed.
puts "Orange" puts "Orange"
The puts
method prints two strings to the console. Each is on its own line. The method includes automatically the newline character.
$ ./printing.rb Apple Apple Orange Orange
This the output of the printing.rb script file.
According to the Ruby documentation, the print
method is an equivalent to the $stdout.print
. The $stdout
is a global variable which holds the standard output stream.
#!/usr/bin/ruby $stdout.print "Ruby language\n" $stdout.puts "Python language"
We print two lines using the $stdout
variable.
Ruby has another three methods for printing output.
#!/usr/bin/ruby p "Lemon" p "Lemon" printf "There are %d apples\n", 3 putc 'K' putc 0xA
In the example, we present the p
, printf
and putc
methods.
p "Lemon"
The p
calls the inspect
method upon the object being printed. The method is useful for debugging.
printf "There are %d apples\n", 3
The printf
method is well known from the C programming language. It enables string formatting.
putc 'K' putc 0xA
The putc
method prints one character to the console. The second line prints a newline. The 0xA
is a hexadecimal code for the newline character.
$ ./printing3.rb "Lemon" "Lemon" There are 3 apples K
This is the output of the printing3.rb
program.
Printing data to the console using the kernel methods is a shortcut: a convenient way to print data. The following example shows a more formal way to print data to the terminal.
ios = IO.new STDOUT.fileno ios.write "ZetCode\n" ios.close
In the example, we open a standard output stream and write a string into it.
ios = IO.new STDOUT.fileno
The new
method returns a stream to which we can write data. The method takes a numeric file descriptor. The STDOUT.fileno
gives us the file descriptor for the standard output stream. We could also simply write 2.
ios.write "ZetCode\n"
We write a string to the opened stream.
ios.close
The input stream is closed.
On Unix systems the standard terminal output is connected to a special file called /dev/tty
. By opening it and writing to it, we write to a console.
#!/usr/bin/ruby fd = IO.sysopen "/dev/tty", "w" ios = IO.new(fd, "w") ios.puts "ZetCode" ios.close
A small example in which we write to a /dev/tty
file. This only works on Unix.
fd = IO.sysopen "/dev/tty", "w"
The sysopen
method opens the given path, returning the underlying file descriptor number.
ios = IO.new(fd, "w")
The file descriptor number is used to open a stream.
ios.puts "ZetCode" ios.close
We write a string to the stream and close it.
Reading input from console
In this section, we will create some code examples that will deal with reading from the console.
The $stdin
is a global variable that holds a stream for the standard input. It can be used to read input from the console.
#!/usr/bin/ruby inp = $stdin.read puts inp
In the above code, we use the read
method to read input from the console.
inp = $stdin.read
The read
method reads data from the standard input until it reaches the end of the file. EOF is produced by pressing Ctrl+D on Unix and Ctrl+Z on Windows.
$ ./reading.rb Ruby language Ruby language
When we launch a program without a parameter, the script reads data from the user. It reads until we press Ctrl+D or Ctrl+Z.
$ echo "ZetCode" | ./reading.rb ZetCode $ ./input.rb < stones Garnet Topaz Opal Amethyst Ruby Jasper Pyrite Malachite Quartz
The script can read data from another program or a file if we do some redirections.
The common way to read data from the console is to use the gets
method.
#!/usr/bin/ruby print "Enter your name: " name = gets puts "Hello #{name}"
We use the gets
method to read a line from the user.
name = gets
The gets
method reads a line from the standard input. The data is assigned to the name variable.
puts "Hello #{name}"
The data that we have read is printed to the console. We use interpolation to include the variable in the string.
$ ./readline.rb Enter your name: Jan Hello Jan
Sample output.
In the following two scripts, we will discuss the chomp
method. It is a string method which removes white spaces from the end of the string. It is useful when doing input operations. The method name and usage comes from the Perl language.
#!/usr/bin/ruby print "Enter a string: " inp = gets puts "The string has #{inp.size} characters"
We read a string from a user and calculate the length of the input string.
$ ./nochomp.rb Enter a string: Ruby The string has 5 characters
The message says that the string has 5 characters. It is because it counts the newline as well.
To get the correct answer, we need to remove the newline character. This is a job for the chomp
method.
#!/usr/bin/ruby print "Enter a string: " inp = gets.chomp puts "The string has #{inp.size} characters"
This time we use we cut the newline character with the chomp
method.
$ ./chomp.rb Enter a string: Ruby The string has 4 characters
The Ruby string has indeed 4 characters.
Files
From the official Ruby documentation we learn that the IO
class is the basis for all input and output in Ruby. The File
class is the only subclass of the IO
class. The two classes are closely related.
#!/usr/bin/ruby f = File.open('output.txt', 'w') f.puts "The Ruby tutorial" f.close
In the first example, we open a file and write some data to it.
f = File.open('output.txt', 'w')
We open a file 'output.txt' in write mode. The open
method returns an io stream.
f.puts "The Ruby tutorial"
We used the above opened stream to write some data. The puts
method can be used to write data to a file as well.
f.close
At the end the stream is closed.
$ ./simplewrite.rb $ cat output.txt The Ruby tutorial
We execute the script and show the contents of the output.txt
file.
We will have a similar example which will show additional methods in action.
#!/usr/bin/ruby File.open('langs', 'w') do |f| f.puts "Ruby" f.write "Java\n" f << "Python\n" end
If there is a block after the open
method then Ruby passes the opened stream to this block. At the end of the block, the file is automatically closed.
f.puts "Ruby" f.write "Java\n" f << "Python\n"
We use three different methods to write to a file.
$ ./simplewrite2.rb $ cat langs Ruby Java Python
We execute the script and check the contents of the langs
file.
In the second example, we show a few methods of the File
class.
#!/usr/bin/ruby puts File.exists? 'tempfile' f = File.new 'tempfile', 'w' puts File.mtime 'tempfile' puts f.size File.rename 'tempfile', 'tempfile2' f.close
The example creates a new file named tempfile
and calls some methods.
puts File.exists? 'tempfile'
The exists?
method checks if a file with a given name already exists. The line returns false, because we have not yet created the file.
f = File.new 'tempfile', 'w'
The file is created.
puts File.mtime 'tempfile'
The mtime
method gives us the last modification time of the file.
puts f.size
We determine the file size. The method returns 0, since we have not written to the file.
File.rename 'tempfile', 'tempfile2'
Finally, we rename the file using the rename
method.
$ ./testfile.rb false 2011-11-05 16:19:36 +0100 0
This is a sample output.
Next, we will be reading data from the files on the disk.
#!/usr/bin/ruby f = File.open("stones") while line = f.gets do puts line end f.close
This is a simple script that will open a file called stones and print it contents line by line to the terminal.
f = File.open("stones")
We open a stones
file. The default mode is a read mode. The stones
file contains nine names of valued stones, each on a separate line.
while line = f.gets do puts line end
The gets
method reads a line from the I/O stream. The while block ends when we reach the end of file.
$ ./readlines2.rb Garnet Topaz Opal Amethyst Ruby Jasper Pyrite Malachite Quartz
This is the output of the example.
The next example will read data from a file.
#!/usr/bin/ruby fname = 'alllines.rb' File.readlines(fname).each do |line| puts line end
This script shows another way of reading a file's contents. The code example will print its own code to the terminal.
File.readlines(fname).each do |line| puts line end
The readlines
reads all lines from the specified file and returns them in the form of an array. We go through the array with the each
method and print the lines to the terminal.
$ ./alllines.rb #!/usr/bin/ruby fname = 'alllines.rb' File.readlines(fname).each do |line| puts line end
Output of the example.
Directories
In this section, we work with directories. We have a Dir
class to work with directories in Ruby.
#!/usr/bin/ruby Dir.mkdir "tmp" puts Dir.exists? "tmp" puts Dir.pwd Dir.chdir "tmp" puts Dir.pwd Dir.chdir '..' puts Dir.pwd Dir.rmdir "tmp" puts Dir.exists? "tmp"
In the script we use four methods of the Dir
class.
Dir.mkdir "tmp"
The mkdir
method makes a new directory called tmp
.
puts Dir.exists? "tmp"
With the exists?
method, we check if a directory with a given name exists in the filesystem.
puts Dir.pwd
The pwd
method prints a current working directory. This is the directory from which we launched the script.
Dir.chdir '..'
The chdir
method changes to another directory. The ..
directory is the parent directory of the current working directory.
Dir.rmdir "tmp" puts Dir.exists? "tmp"
Finally, we remove a directory with the rmdir
method. This time the exists?
method returns false.
$ ./dirs.rb true /home/vronskij/programming/ruby/io /home/vronskij/programming/ruby/io/tmp /home/vronskij/programming/ruby/io false
This is the output of the example.
In the second example, we retrieve all of a directory's entries, including its files and subdirectories.
#!/usr/bin/ruby fls = Dir.entries '.' puts fls.inspect
The entries
method returns all entries of a given diretory.
fls = Dir.entries '.' puts fls.inspect
We get the array of files and directories of a current directory. The .
character stands for the current working directory in this context. The inspect
method gives us a more readable representation of the array.
$ ./allfiles.rb ["putc.rb", "simplewrite.rb", "readlines2.rb", "fileexists.rb~" ...
In the output we can see an array of files and directories.
The third example works with a home directory. Every user in a computer has a unique directory assigned to him. It is called a home directory. It is a place where he can place his files and create his own hierarchy of directories.
#!/usr/bin/ruby puts Dir.home puts Dir.home 'root'
The script prints two home directories.
puts Dir.home
If we do not specify the user name, then a home directory of a current user is returned. The current user is the owner of the script file. Someone, who has launched the script.
puts Dir.home 'root'
Here we print the home directory of a specific user: in our case, the superuser.
$ ./homedir.rb /home/vronskij /root
This is a sample output.
Executing external programs
Ruby has several ways to execute external programs. We will deal with some of them. In our examples we will use well known Linux commands. Readers with Windows or Mac can use commands specific for their systems.
#!/usr/bin/ruby data = system 'ls' puts data
We call a ls
command which lists directory contents.
data = system 'ls'
The system
command executes an external program in a subshell. The method belongs to the Kernel
Ruby module.
$ ./system.rb allfiles.rb characters.rb fileexists.rb homedir.rb~ ...
This is a sample output.
We show two other ways of running external programs in Ruby.
#!/usr/bin/ruby out = `pwd` puts out out = %x[uptime] puts out out = %x[ls | grep 'readline'] puts out
To run external programs we can use backticks ``
or %x[]
characters.
out = `pwd`
Here we execute the pwd
command using backticks. The command returns the current working directory.
out = %x[uptime]
Here we get the output of the uptime
command, which tells how long a system is running.
out = %x[ls | grep 'readline']
We can use also a combination of commands.
$ ./system2.rb /home/vronskij/programming/ruby/io 22:50:50 up 5:32, 1 user, load average: 0.46, 0.44, 0.45 readline.rb readline.rb~ readlines2.rb readlines2.rb~
This is a sample output.
We can execute a command with the open
method. The method belongs to the Kernel
module. It creates an IO object connected to the given stream, file, or subprocess. If we want to connect to a subprocess, we start the path of the open
with a pipe character |
.
#!/usr/bin/ruby f = open("|ls -l |head -3") out = f.read puts out f.close puts $?.success?
In the example, we print the outcome of the ls -l | head -3
commands. The combination of these two commands returns the first three lines of the ls -l
command. We also check the status of the child subprocess.
f = open("|ls -l |head -3")
We connect to a subprocess, created by these two commands.
out = f.read puts out
We read and print data from the subprocess.
f.close
We close the file handler.
puts $?.success?
The $?
is a special Ruby variable that is set to the status of the last executed child process. The success?
method returns true if the child process ended OK.
$ ./system3.rb total 148 -rwxr-xr-x 1 vronskij vronskij 57 2011-10-30 23:33 allfiles.rb -rwxr-xr-x 1 vronskij vronskij 58 2011-10-30 23:33 allfiles.rb~ true
This is a sample output.
Redirecting standard output
Ruby has predefined global variables for standard input, standard output and standard error output. The $stdout
is the name of the variable for the standard output.
#!/usr/bin/ruby $stdout = File.open "output.log", "a" puts "Ruby" puts "Java" $stdout.close $stdout = STDOUT puts "Python"
In the above example, we redirect a standard output to the output.log file.
$stdout = File.open "output.log", "a"
This line creates a new standard ouput. The standard output will now flow to the ouput.log
file. The file is opened in the append mode. If the file does not exist yet, it is created. Otherwise it is opened and the data is written at the end of the file.
puts "Ruby" puts "Java"
We print two strings. The strings will not be shown in the terminal as usual. Rather, they will be appended to the output.log
file.
$stdout.close
The handler is closed.
$stdout = STDOUT puts "Python"
We use a predefined standard constant STDOUT
to recreate the normal standard ouput. The "Python" string is printed to the console.
In this part of the Ruby tutorial, we worked with input and output operations in Ruby.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论