Ruby's input and output and standard library

Source: Internet
Author: User
Tags documentation socket

Input and output

In all this series of articles, a large number of Ruby standard output methods are used when writing Ruby code. Among them, the most common is the print and puts methods, the details of its use no longer repeat.

All of these and other methods for processing input and output are defined in the kernel module. This kernel module is also included in the object class. Therefore, the kernel method appears in each object. In terms of output, kernel defines the PRINT,PRINTF,PUTC and IO classes and two subclasses (file and Basicsocket)-they allow you to read and write files and sockets. Basicsocket is part of the socket library and will be discussed later. Contains the Filetest module's file class, which provides a number of methods for operating system files and directories. Methods used in kernel to read and write to the standard input/output mechanism are further reused for read and write operations in the file instance. Here's a code example-it writes some names to a new file, and then reads the names back into an array.

customers=%w[Jim Kevin Davin Andrew]
outFile = File.new("c:\\examples\\test\\customers.txt", "w")
customers.each{|customer| outFile.puts(customer)}
outFile.close
inFile= File.new("c:\\examples\\customers.txt", "r")
readCustomers=inFile.readlines
readCustomers.each{|customer| puts customer}
inFile.close

Standard library

In addition to providing a large number of built-in classes and modules, Ruby also provides a certain number of standard libraries. These libraries are not automatically part of the Ruby classes, modules, and methods that you can take advantage of. You must first use the require (or load) keyword at the top of your file to use the class or module in the library. In the previous section, I mentioned a library-socket font that contained a large number of ruby classes (including basicsocket) to facilitate access to network services. But in Ruby downloads, a whole set of other libraries is provided together. You can look at the Lib directory in your Ruby download, which should have a lot of libraries that your Ruby program needs to use.

The bad thing about these libraries is that there's not a lot of reference documentation for these classes. You can find a list of standard libraries and their files containing classes and modules on the site http://www.ruby-doc.org/stdlib/. Even these documents indicate:

"You need to understand that the bold library in the table has a good document, and the Italic library has no documentation." "

This is the status of Ruby. You might want to say that Ruby is an incredibly rich and powerful language and has many of the features built into our applications, but the documentation is still a little inadequate. Fortunately, there are already a lot of people trying to improve Ruby's documentation and support. There are a number of ruby forums that are already stubborn, and with each new release, the documentation will be improved-and, of course, it's been the result of recent attention. However, help documents can still be a factor in the language's frustration.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.