f=File.new(File.join("C:","Test.txt"), "w+")
f.puts("I am Jack")
f.puts("Hello World")
文件模式
"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 existsotherwise, creates a new file for writing.
"a+" :Read-write. Starts at end of file if file existsotherwise, 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=File.open(File.join("C:","Test.txt"),"r")
file.each { |line| print "#{file.lineno}.", line }
file.close
三、新建、删除、重命名文件
File.new( "books.txt", "w" )
File.rename( "books.txt", "chaps.txt" )
File.delete( "chaps.txt" )
四、目录操作
1 创建目录
Dir.mkdir("c:/testdir")
04 #删除目录
05 Dir.rmdir("c:/testdir")
07 #查询目录里的文件
08 p Dir.entries(File.join("C:","Ruby")).join(' ')
10 #遍历目录
11 Dir.entries(File.join("C:","Ruby")).each {
|e| puts e
}
1、ARGV and ARGF
ARGV
ARGV <<"cnblogslink.txt"
#The gets method is a Kernel method that gets lines from ARGV
print while gets
p ARGV.class
ARGF
while line = ARGF.gets
print line
end
2、文件信息查询
#文件是否存在
p File::exists?( "cnblogslink.txt" ) # =>true
#是否是文件
p File.file?( "cnblogslink.txt" ) # =>true
#是否是目录
p File::directory?( "c:/ruby" ) # =>true
p File::directory?( "cnblogslink.txt" ) # =>false
#文件权限
p File.readable?( "cnblogslink.txt" ) # =>true
p File.writable?( "cnblogslink.txt" ) # =>true
p File.executable?( "cnblogslink.txt" ) # =>false
#是否是零长度
p File.zero?( "cnblogslink.txt" ) # =>false
#文件大小 bytes
p File.size?( "cnblogslink.txt" ) # =>74
p File.size( "cnblogslink.txt" ) # =>74
#文件或文件夹
p File::ftype( "cnblogslink.txt" ) # =>"file"
#文件创建、修改、最后一次存取时间
p File::ctime( "cnblogslink.txt" ) # =>Sat Sep 19 08:05:07 +0800 2009
p File::mtime( "cnblogslink.txt" ) # =>Sat Sep 19 08:06:34 +0800 2009
p File::atime( "cnblogslink.txt" ) # =>Sat Sep 19 08:05:07 +0800 2009
3、查找文件
puts "查找目录下所有文件及文件夹"
Dir["c:/ruby/*"].each {|x|
puts x
}
puts "条件查询"
Dir.foreach('c:/ruby') {
|x| puts x if x != "." &&x != ".."
}
puts "查找某一类型文件"
Dir["*.rb"].each {|x|
puts x
}
puts "Open 查询"
Dir.open('c:/ruby') { |d| d.grep /l/ }.each{|x| puts x}
puts "---------------------------"
puts "正则表达式查询"
Dir["c:/ruby/ruby/[rs]*"].each{|x| puts x}
puts "------------------------"
Dir["c:/ruby/[^s]*"].each{|x| puts x}
puts "------------------------"
Dir["c:/ruby/{ruby,li}*"].each{|x| puts x}
puts "------------------------"
Dir["c:/ruby/?b*"].each{|x| puts x}
puts "查找目录及子目录的文件"
require 'find'
Find.find('./') { |path| puts path }
3、查询目录及子目录文件
require "find"
Find.find("/etc/passwd", "/var/spool/lp1", ".") do |f|
Find.prune if f == "."
puts f
end
原型:ref.find( [ aName ]* ) {| aFileName | block }
prune:Skips the current file or directory, restarting the loop with the next entry. If the current file is a directory, that directory will not be recursively entered. Meaningful only within the block associated with Find::find.
4、文件比较 复制等
require 'ftools'
File.copy 'testfile', 'testfile1' » true
File.compare 'testfile', 'testfile1' » true
步骤0 - 安装系统需要的包
# For Mac
# 先安装 [Xcode](http://developer.apple.com/xcode/) 开发工具,它将帮你安装好 Unix 环境需要的开发包
步骤1 - 安装 RVM
RVM 是干什么的这里就不解释了,后面你将会慢慢搞明白。
$ curl -L https://get.rvm.io | bash -s stable
期间可能会问你sudo管理员密码,以及自动通过homebrew安装依赖包,等待一段时间后就可以成功安装好 RVM。
然后,载入 RVM 环境(新开 Termal 就不用这么做了,会自动重新载入的)
$ source ~/.rvm/scripts/rvm
检查一下是否安装正确
$ rvm -v
rvm 1.22.17 (stable) by Wayne E. Seguin [email protected]>, Michal Papis [email protected]>[https://rvm.io/]
步骤2 - 用 RVM 安装 Ruby 环境
列出已知的ruby版本
$ rvm list known
可以选择现有的rvm版本来进行安装(下面以rvm 2.0.0版本的安装为例)
$ rvm install 2.0.0
同样继续等待漫长的下载,编译过程,完成以后,Ruby, Ruby Gems 就安装好了。
另附:
查询已经安装的ruby
$ rvm list
卸载一个已安装版本
$ rvm remove 1.9.2
步骤3 - 设置 Ruby 版本
RVM 装好以后,需要执行下面的命令将指定版本的 Ruby 设置为系统默认版本
$ rvm 2.0.0 --default
同样,也可以用其他版本号,前提是你有用 rvm install 安装过那个版本
这个时候你可以测试是否正确
$ ruby -v
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin13.0.0]
$ gem -v
2.1.6
这有可能是因为Ruby的默认源使用的是cocoapods.org,国内访问这个网址有时候会有问题,网上的一种解决方案是将远替换成淘宝的,替换方式如下:
$gem source -r https://rubygems.org/
$ gem source -a https://ruby.taobao.org
要想验证是否替换成功了,可以执行:
$ gem sources -l
正常的输出结果:
CURRENT SOURCES
http://ruby.taobao.org/
到这里就已经把Ruby环境成功的安装到了Mac OS X上,接下来就可以进行相应的开发使用了。