ruby文件操作Word格式.docx

上传人:b****6 文档编号:17310508 上传时间:2022-12-01 格式:DOCX 页数:27 大小:26.20KB
下载 相关 举报
ruby文件操作Word格式.docx_第1页
第1页 / 共27页
ruby文件操作Word格式.docx_第2页
第2页 / 共27页
ruby文件操作Word格式.docx_第3页
第3页 / 共27页
ruby文件操作Word格式.docx_第4页
第4页 / 共27页
ruby文件操作Word格式.docx_第5页
第5页 / 共27页
点击查看更多>>
下载资源
资源描述

ruby文件操作Word格式.docx

《ruby文件操作Word格式.docx》由会员分享,可在线阅读,更多相关《ruby文件操作Word格式.docx(27页珍藏版)》请在冰豆网上搜索。

ruby文件操作Word格式.docx

).stat.zero?

flag=File.new("

这个将会返回true,这是因为在ruby中0也是true,nil才是false.

所以我们可以使用size?

方法:

1.ifFile.new("

myfile"

).stat.size?

2.puts"

Thefilehascontents."

3.else

4.puts"

Thefileisempty."

5.end

ifFile.new("

puts"

else

end

FileTest模块里面也有zero?

和size?

1.flag1=FileTest:

zero?

file1"

2.flag2=FileTest:

size?

file2"

flag1=FileTest:

flag2=FileTest:

这里还有一个size方法:

1.size1=File.size("

2.size2=File.stat("

).size

size1=File.size("

size2=File.stat("

).size

2检测特殊文件属性

这边要注意,File类mix了FIleTest模块,并且FileTest模块和File:

Stat模块功能上也有很多重复.

unix/linux有面向字符和面向块的设备。

FileTest的方法blockdev?

和chardev?

可以进行测试:

chardev?

/dev/hdisk0"

)#false

blockdev?

)#true

)#false

)#false

)#true

有时我们想要知道一个流是否联系到了终端,这时我们可以使用IO类的tty?

1.flag1=STDIN.tty?

#true

2.flag2=File.new("

diskfile"

).isatty#false

#true

flag1=STDIN.tty?

#true

flag2=File.new("

).isatty#false

一个流可以是一个管道,或者一个socket:

pipe?

(myfile)

socket?

(myfile)

要区分目录和普通文件我们这样使用:

1.file1=File.new("

/tmp"

2.file2=File.new("

/tmp/myfile"

3.test1=file1.directory?

4.test2=file1.file?

#false

5.test3=file2.directory?

6.test4=file2.file?

#false

file1=File.new("

file2=File.new("

test1=file1.directory?

test2=file1.file?

#false

test3=file2.directory?

test4=file2.file?

File还有一个类方法ftype,他将返回流的类型.他也在File:

Stat里面,只不过是实例方法.它的返回值可能是下面的字符

串(file、directory、blockSpecial、characterSpecial、fifo、link或socket).

1.this_kind=File.ftype("

)#"

blockSpecial"

2.that_kind=File.new("

).stat.ftype#"

directory"

this_kind=File.ftype("

that_kind=File.new("

要测试一个文件是否为另一个文件的链接,可以使用FileTest的symlink?

方法,要计算链接数量,可以使用nlink方法:

1.File.symlink("

yourfile"

"

)#Makealink

2.is_sym=FileTest:

symlink?

)#true

3.hard_count=File.new("

).stat.nlink#0

)#Makealink

File.symlink("

)#Makealink

is_sym=FileTest:

hard_count=File.new("

).stat.nlink#0

3使用管道

ruby中使用IO.popen打开管道:

1.check=IO.popen("

spell"

r+"

2.check.puts("

'

Twasbrillig,andtheslithytoves"

3.check.puts("

Didgyreandgimbleinthewabe."

4.check.close_write

5.list=check.readlines

6.list.collect!

{|x|x.chomp}

7.#listisnow%w[brilliggimblegyreslithytoveswabe]

check=IO.popen("

check.puts("

check.close_write

list=check.readlines

list.collect!

{|x|x.chomp}

#listisnow%w[brilliggimblegyreslithytoveswabe]

要注意必须调用close_write,如果没有调用它,读取管道的时候,就不能到达文件的末尾.

下面是一个block的形式:

1.File.popen("

/usr/games/fortune"

)do|pipe|

2.quote=pipe.gets

3.putsquote

4.#Onacleandisk,youcanseekforever.-ThomasSteel

4.#Onacleandisk,youcanseekforever.-ThomasSteel

File.popen("

)do|pipe|

quote=pipe.gets

putsquote

#Onacleandisk,youcanseekforever.-ThomasSteel

如果指定了一个字符串"

-"

,那么一个新的ruby实例将被创建.如果指定了一个block,那么这个block将会作为两个独立

的进程运行。

子进程得到nil,父进程得到一个IO对象:

1.IO.popen("

)do|mypipe|

2.ifmypipe

3.puts"

I'

mtheparent:

pid=#{Process.pid}"

4.listen=mypipe.gets

5.putslisten

6.else

7.puts"

mthechild:

8.end

9.end

10.

11.#Prints:

12.#I'

pid=10580

13.#I'

pid=10582

pid=10580

IO.popen("

)do|mypipe|

ifmypipe

listen=mypipe.gets

putslisten

else

end

#Prints:

#I'

pid=10580

pid=10582

pipe方法也返回互相连接的一对管道:

1.pipe=IO.pipe

2.reader=pipe[0]

3.writer=pipe[1]

4.

5.str=nil

6.thread1=Thread.new(reader,writer)do|reader,writer|

7.#writer.close_write

8.str=reader.gets

9.reader.close

10.end

11.

12.thread2=Thread.new(reader,writer)do|reader,writer|

13.#reader.close_read

14.writer.puts("

WhathathGodwrought?

"

15.writer.close

16.end

17.

18.thread1.join

19.thread2.join

20.

21.putsstr#WhathathGodwrought?

7.#writer.close_write

13.#reader.close_read

pipe=IO.pipe

reader=pipe[0]

writer=pipe[1]

str=nil

thread1=Thread.new(reader,writer)do|reader,writer|

#writer.close_write

str=reader.gets

reader.close

thread2=Thread.new(reader,writer)do|reader,writer|

#reader.close_read

writer.puts("

writer.close

thread1.join

thread2.join

putsstr#WhathathGodwrought?

4使用非阻塞IO

ruby会在后台执行一些操作,使io不会被阻断,因此大部分情况下可以使用ruby线程来管理IO,当一个线程被Io阻塞之

后,另外的线程能够继续执行.

由于ruby的线程不是一个native的线程,因此ruby的线程都在同一个进程里面.

如果你想关闭一个非阻塞io,你可以这样做:

1.require'

io/nonblock'

2.

3.#...

5.test=mysock.nonblock?

6.

7.mysock.nonblock=true#turnoffblocking

8.#...

9.mysock.nonblock=false#turnonagain

11.mysock.nonblock{some_operation(mysock)}

12.#Performsome_operationwithnonblockingsettotrue

13.

14.mysock.nonblock(false){other_operation(mysock)}

15.#Performother_operationwithnon-blockingsettofalse

3.#...

7.mysock.nonblock=true#turnoffblocking

8.#...

9.mysock.nonblock=false#turnonagain

12.#Performsome_operationwithnonblockingsettotrue

require'

#...

test=mysock.nonblock?

mysock.nonblock=true#turnoffblocking

mysock.nonblock=false#turnonagain

mysock.nonblock{some_operation(mysock)}

#Performsome_operationwithnonblockingsettotrue

mysock.nonblock(false){other_operation(mysock)}

#Performother_operationwithnon-blockingsettofalse

5使用readpartial

readpartial被设计来用于就像socket这样的流.

readpartial要求提供最大长度的参数,如果指定了buffer,那么这个buffer应指向用于存储数据的一个字符串。

1.data=sock.readpartial(128)#Readatmost128bytes

data=sock.readpartial(128)#Readatmost128bytes

readpartial

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高中教育 > 初中教育

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1