ruby文件操作
1、文件的打开与关闭
``r'' Read-only, starts at beginning of file (default mode).
``r+'' Read-write, starts at beginning of file.
``w'' Write-only, truncates existing file to zero length or creates a new file for writing.
``w+'' Read-write, truncates existing file to zero length or creates a new file for reading and writing.
``a'' Write-only, starts at end of file if file exists, otherwise creates a new file for writing.
``a+'' Read-write, starts at end of file if file exists, otherwise creates a new file for reading and writing.
``b'' (DOS/Windows only) Binary file mode (may appear with any of the key letters listed above).
使用file.new方法获取一个文件句柄来对文件操作,操作结束后file.close来关闭文件。
file.open方法是new方法的扩充,该方法可有代码块,该代码块结束后自动close,而且在操作过程中发生错误时能够自动收集错误并推出
如
file.open("filepath") do |file|
file.each do |line| ... end
end
一些文件的常用命令:
File.open(dir+"/read.txt","w") do |file|
file.puts("djkjsadlkjdkdsfdsee")
end puts File.exists?(dir+"/read.txt") 文件是否存在
puts File.directory?(dir+"/read.txt") 文件是否是目录路径
puts File.file?(dir+"/read.txt") 是否是文件
puts File.zero?(dir+"/read.txt") 文件内容长度是否为0
puts File.size(dir+"/read.txt") 获取文件大小
put
相关文档:
英文资源:
http://www.ruby-lang.org/
http://www.ruby-doc.org(访问不到可用下面的网址访问):
http://anonymouse.org/cgi-bin/anon-www.cgi/http://www.ruby-doc.org
http://rubyforge.org/
http://www.ruby-forum.org/bb
http://www.rubygarden.org/ruby
http://www.rubyxml.com/
http://www.pragmaticprogra ......
1:FXRuby is a library for developing powerful and sophisticated cross-platform graphical user interfaces (GUIs) for your Ruby applications. It’s based on the FOX Toolkit, a popular open source C++ library developed by Jeroen van der Zijp. What that means for you as an application developer is that ......
Ruby虽然是动态脚本语言,但是和Java一样,带有VM,有自己的内存堆,创建对象的时候在堆里面分配内存,对象使用完毕由GC进行回收。但是通过我们运营Rails网站两年多的实践来看,Ruby VM的GC还是存在很大的问题。简单的来说,就是GC之后,尽管对象已经完全回收,但是物理内存释放不够充分,有泄漏的现象。通过pmap来dump rub ......
有些时候可能会根据一些有限的信息,来查找页面的元素,这里举一个例子利用页面文字来查找所在的标签,以淘宝的登录页面为例,使用以下代码可以实现根据账户名来识别对应的节点名称:
require ‘watir’
#ie = Watir::IE.attach(:url, /member1.taobao.com/)
ie = Watir::IE.start(”http://memb ......