国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

direktori cari
Ruby用戶指南 3、開始 4、簡單的例子 5、字符串 6、正則表達(dá)式 7、數(shù)組 8、回到那些簡單的例子 9、流程控制 10、迭代器 11、面向?qū)ο笏季S 12、方法 13、類 14、繼承 15、重載方法 16、訪問控制 17、單態(tài)方法 18、模塊 19、過程對象 20、變量 21、全局變量 22、實(shí)變量 23、局部變量 24、類常量 25、異常處理:rescue 26、異常處理:ensure 27、存取器 28、對象的初始化 29、雜項(xiàng) RGSS入門教程 1、什么是RGSS 2、開始:最簡單的腳本 3、數(shù)據(jù)類型:數(shù)字 4、數(shù)據(jù)類型:常量與變量 5、數(shù)據(jù)類型:字符串 6、控制語句:條件分歧語句 7、控制語句:循環(huán) 8、函數(shù) 9、對象與類 10、顯示圖片 11、數(shù)組 12、哈希表(關(guān)聯(lián)數(shù)組) 13、類 14、數(shù)據(jù)庫 15、游戲?qū)ο?/a> 16、精靈的管理 17、窗口的管理 18、活動指令 19、場景類 Programming Ruby的翻譯 Programming Ruby: The Pragmatic Programmer's Guide 前言 Roadmap Ruby.new 類,對象和變量 容器Containers,塊Blocks和迭代Iterators 標(biāo)準(zhǔn)類型 深入方法 表達(dá)式Expressions 異常,捕捉和拋出(已經(jīng)開始,by jellen) 模塊 基本輸入輸出 線程和進(jìn)程 當(dāng)遭遇挫折 Ruby和它的世界 Ruby和Web開發(fā) Ruby Tk Ruby 和微軟的 Windows 擴(kuò)展Ruby Ruby語言 (by jellen) 類和對象 (by jellen) Ruby安全 反射Reflection 內(nèi)建類和方法 標(biāo)準(zhǔn)庫 OO設(shè)計(jì) 網(wǎng)絡(luò)和Web庫 Windows支持 內(nèi)嵌文檔 交互式Ruby Shell 支持 Ruby參考手冊 Ruby首頁 卷首語 Ruby的啟動 環(huán)境變量 對象 執(zhí)行 結(jié)束時(shí)的相關(guān)處理 線程 安全模型 正則表達(dá)式 字句構(gòu)造 程序 變量和常數(shù) 字面值 操作符表達(dá)式 控制結(jié)構(gòu) 方法調(diào)用 類/方法的定義 內(nèi)部函數(shù) 內(nèi)部變量 內(nèi)部常數(shù) 內(nèi)部類/模塊/異常類 附加庫 Ruby變更記錄 ruby 1.6 特性 ruby 1.7 特性 Ruby術(shù)語集 Ruby的運(yùn)行平臺 pack模板字符串 sprintf格式 Marshal格式 Ruby FAQ Ruby的陷阱
watak

基本輸入輸出



Ruby提供了兩種I/O例程,第一種是簡單的接口,我們已經(jīng)用了很多了。

print?"Enter?your?name:?"
name?=?gets

Kernel模塊提供了一整套和I/O相關(guān)的方法:gets, open, print, printf, putc, puts, readline, readlines, 和test等,這些方法能使你簡單方便的進(jìn)行Ruby編程。這些方法典型的對標(biāo)準(zhǔn)輸入輸出進(jìn)行操作,很適合編寫過濾器等。

另一方面,ruby提供了IO對象,讓我們可以對io進(jìn)行更多的控制。

什么是IO對象?

Ruby defines a single base class, IO, to handle input and output. This base class is subclassed by classes File and BasicSocket to provide more specialized behavior, but the principles are the same throughout. An IO object is a bidirectional channel between a Ruby program and some external resource.[For those who just have to know the implementation details, this means that a single IO object can sometimes be managing more than one operating system file descriptor. For example, if you open a pair of pipes, a single IO object contains both a read pipe and a write pipe.] There may be more to an IO object than meets the eye, but in the end you still simply write to it and read from it.

In this chapter, we'll be concentrating on class IO and its most commonly used subclass, class File. For more details on using the socket classes for networking, see the section beginning on page 469.

Opening and Closing Files

As you might expect, you can create a new file object using File.new .

aFile?=?File.new("testfile",?"r")

#?...?process?the?file

aFile.close

You can create a File object that is open for reading, writing, or both, according to the mode string (here we opened ``testfile'' for reading with an ``r''). The full list of allowed modes appears on page 326. You can also optionally specify file permissions when creating a file; see the description of File.new on page 303 for details. After opening the file, we can work with it, writing and/or reading data as needed. Finally, as responsible software citizens, we close the file, ensuring that all buffered data is written and that all related resources are freed.

But here Ruby can make life a little bit easier for you. The method File.open also opens a file. In regular use, it behaves just like File.new . However, if there's a block associated with the call, open behaves differently. Instead of returning a new File object, it invokes the block, passing the newly opened File as a parameter. When the block exits, the file is automatically closed.

File.open("testfile",?"r")?do?|aFile|

#?...?process?the?file

end

Reading and Writing Files

The same methods that we've been using for ``simple'' I/O are available for all file objects. So, gets reads a line from standard input, and aFile.gets reads a line from the file object aFile.

However, I/O objects enjoy an additional set of access methods, all intended to make our lives easier.

Iterators for Reading

As well as using the usual loops to read data from an IO stream, you can also use various Ruby iterators. IO#each_byte invokes a block with the next 8-bit byte from the IO object (in this case, an object of type File).

aFile?=?File.new("testfile")
aFile.each_byte?{|ch|?putc?ch;?putc??.?}
produces:
T.h.i.s.?.i.s.?.l.i.n.e.?.o.n.e.
.T.h.i.s.?.i.s.?.l.i.n.e.?.t.w.o.
.T.h.i.s.?.i.s.?.l.i.n.e.?.t.h.r.e.e.
.A.n.d.?.s.o.?.o.n.......
.

IO#each_line calls the block with the next line from the file. In the next example, we'll make the original newlines visible using String#dump , so you can see that we're not cheating.

aFile.each_line?{|line|?puts?"Got?#{line.dump}"?}
produces:
Got?"This?is?line?one\n"
Got?"This?is?line?two\n"
Got?"This?is?line?three\n"
Got?"And?so?on...\n"

You can pass each_line any sequence of characters as a line separator, and it will break up the input accordingly, returning the line ending at the end of each line of data. That's why you see the ``\n'' characters in the output of the previous example. In the next example, we'll use ``e'' as the line separator.

aFile.each_line("e")?do?|line|
??puts?"Got?#{?line.dump?}"
end
produces:
Got?"This?is?line"
Got?"?one"
Got?"\nThis?is?line"
Got?"?two\nThis?is?line"
Got?"?thre"
Got?"e"
Got?"\nAnd?so?on...\n"

If you combine the idea of an iterator with the auto-closing block feature, you get IO.foreach . This method takes the name of an I/O source, opens it for reading, calls the iterator once for every line in the file, and then closes the file automatically.

IO.foreach("testfile")?{?|line|?puts?line?}
produces:
This?is?line?one
This?is?line?two
This?is?line?three
And?so?on...

Or, if you prefer, you can retrieve an entire file into an array of lines:

arr?=?IO.readlines("testfile")
arr.length ?/td> 4
arr[0] ?/td> "This?is?line?one\n"

Don't forget that I/O is never certain in an uncertain world---exceptions will be raised on most errors, and you should be ready to catch them and take appropriate action.

Writing to Files

So far, we've been merrily calling puts and print, passing in any old object and trusting that Ruby will do the right thing (which, of course, it does). But what exactly is it doing?

The answer is pretty simple. With a couple of exceptions, every object you pass to puts and print is converted to a string by calling that object's to_s method. If for some reason the to_s method doesn't return a valid string, a string is created containing the object's class name and id, something like <ClassName:0x123456>.

The exceptions are simple, too. The nil object will print as the string ``nil,'' and an array passed to puts will be written as if each of its elements in turn were passed separately to puts.

What if you want to write binary data and don't want Ruby messing with it? Well, normally you can simply use IO#print and pass in a string containing the bytes to be written. However, you can get at the low-level input and output routines if you really want---have a look at the documentation for IO#sysread and IO#syswrite on page 335.

And how do you get the binary data into a string in the first place? The two common ways are to poke it in byte by byte or to use Array#pack .

str?=?"" ?/td> ""
str?<<?1?<<?2?<<?3 ?/td> "\001\002\003"
[?4,?5,?6?].pack("c*") ?/td> "\004\005\006"

But I Miss My C++ Iostream

Sometimes there's just no accounting for taste...However, just as you can append an object to an Array using the << operator, you can also append an object to an output IO stream:

endl?=?"\n"
$stdout?<<?99?<<?"?red?balloons"?<<?endl
produces:
99?red?balloons

Again, the << method uses to_s to convert its arguments to strings before sending them on their merry way.

與網(wǎng)絡(luò)交互

Ruby支持很多網(wǎng)絡(luò)協(xié)議,不管是高層的還是底層的。

ruby提供了一些基本類,讓你可以使用TCP,UDP,SOCKS等很多協(xié)議交互,而不必拘泥在網(wǎng)絡(luò)層。這些類也提供了輔助類,讓你可以輕松的對服務(wù)器進(jìn)行讀寫。這個(gè)例子利用finger協(xié)議查詢用戶oracle的信息:

require?'socket'
client?=?TCPSocket.open('localhost',?'finger')
client.send("oracle\n",?0)????#?0?means?standard?packet
puts?client.readlines
client.close
結(jié)果:
Login:?oracle?????????			Name:?Oracle?installation
Directory:?/home/oracle?????????????	Shell:?/bin/bash
Never?logged?in.
No?Mail.
No?Plan.

對于高層,lib/net里面提供了一些與應(yīng)用層協(xié)議(FTP,HTTP,POP,SMTP,TELNET)等交互的庫模塊。比如,下面的例子列出了Pragmatic Programmer主頁里的圖像。

require?'net/http'

h?=?Net::HTTP.new('www.pragmaticprogrammer.com',?80) resp,?data?=?h.get('/index.html',?nil) if?resp.message?==?"OK" ??data.scan(/<img?src="(.*?)"/)?{?|x|?puts?x?} end
produces:
images/title_main.gif
images/dot.gif
images/dot.gif
images/dot.gif
images/aafounders_70.jpg
images/pp_cover_thumb.png
images/ruby_cover_thumb.png
images/dot.gif
images/dot.gif


Extracted from the book "Programming Ruby - The Pragmatic Programmer's Guide"
Copyright ? 2001 by Addison Wesley Longman, Inc. This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/)).

Distribution of substantively modified versions of this document is prohibited without the explicit permission of the copyright holder.

Distribution of the work or derivative of the work in any standard (paper) book form is prohibited unless prior permission is obtained from the copyright holder.
Artikel sebelumnya: Artikel seterusnya: