ruby文件夹操作

Python017

ruby文件夹操作,第1张

一、新建文件

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

Ruby是一个完美的面向对象编程语言。拥有面向对象的编程语言的功能,包括:

1、数据封装

2、数据抽象

3、多态性

4、继承

这些功能已在讨论面向对象Ruby

面向对象的程序涉及类和对象。 一个类是蓝本,从个别对象被创建。在面向对象的术语,我们说小明的自行车是被称为自行车类的对象实例。

任何车辆的例子。它包括轮子,马力,燃油或燃气罐容量。这些特点形成的类车辆的数据成员。可以从其他车辆区分这些特征。

车辆也有一定的功能,如停止,驾驶,超速驾驶。即使这些功能形成的类车辆的数据成员。因此,可以定义一个类作为一个组合的特点和功能。

车辆类可以被定义为:

Class Vehicle

{

Number no_of_wheels

Number horsepower

Characters type_of_tank

Number Capacity

Function speeding

{

}

Function driving

{

}

Function halting

{

}

}

通过这些数据成员分配不同的值,可以形成类车辆的几个实例。例如,飞机的有三个轮子,1,000马力,不同的燃料罐及容量为100升。同样的方式,一辆汽车有四个轮子,200马力,气体作为不同的罐及容量25升。

Ruby中定义一个类:

要通过使用Ruby实现面向对象编程,需要先学习如何创建对象和Ruby中的类。

Ruby中一个类总是以关键字class类的名称开头。名称应始终以首字母大写。如以是Customer类可以显示为:

class Customer

end

类定义结束通过使用关键字end结束。在类的所有数据成员是类之间的定义,并以end关键字作为结束符。

Ruby类中的变量:

Ruby提供了四种类型的变量:

局部变量: 局部变量是在一个方法中定义的变量。局部变量是不可用的方法外。更多细节在随后的章节中的方法中会介绍。局部变量一般以小写字母或_开头。

实例变量: 实例变量是可跨越任何特定实例或对象的方法。这意味着,从对象到对象的实例变量改变。实例变量前面加上at符号(@),跟着变量名。

类变量:类变量是可在各种不同的对象。 一个类变量属于类,是类的一个特点。他们前面的符号@@跟着的变量名。

全局变量: 类变量是不能跨类。如果想要一个单一的变量可以跨类,需要定义一个全局变量。全局变量的前面总是用美元符号($)。

例子:

使用类变量@@no_of_customers,能确定创建的对象的数量。这使得导出的客户数量。

class Customer

@@no_of_customers=0

end

Ruby中使用new方法创建对象:

对象是类的实例。现在,将学习如何在Ruby中创建对象一个类对象。Ruby中通过使用new方法创建对象。

new方法是一种独特的方法,这是预定义在Ruby库。new方法属于类的方法。

下面的例子是创建两个对象类客户cust1 和 cust2:

cust1 = Customer. new

cust2 = Customer. new

在这里,cust1和cust2是两个对象的名字。在等于号(=)之后,类名称将按照对象名称。然后,点运算符和关键字new在后面。

自定义方法来创建Ruby对象 :

可以通过new方法的参数,这些参数可以用来初始化类变量。

当打算声明的new方法具有参数,需要声明的方法在创建类的时候初始化。

initialize方法是一种特殊类型的方法,该方法时将执行new方法的类被称为参数。

下面的例子是创建initialize方法:

class Customer

@@no_of_customers=0

def initialize(id, name, addr)

@cust_id=id

@cust_name=name

@cust_addr=addr

end

end

在这个例子中,可以声明局部变量的初始化方法id, name和addr。这里def 结束被用来定义一个Ruby的方法初始化。这些将在有关后续章节中了解更多。

在initialize方法中,对这些局部变量的值传递到实例变量@cust_id,@cust_name和@cust_addr。这里的局部变量持有的值由new方法一同传递。

现在可以创建对象,如下所示:

cust1=Customer.new("1", "John", "Wisdom Apartments, Ludhiya")

cust2=Customer.new("2", "Poul", "New Empire road, Khandala")

Ruby中类的成员函数:

在Ruby中,函数被调用的方法。在一个类中的每个方法的方法名用关键字def开始。

方法名总是以小写字母最好。你最终的方法Ruby中通过使用关键字end表示结束。

下面的例子是定义一个Ruby的方法:

class Sample

def function

statement 1

statement 2

end

end

这里statement1和statement2为函数体的一部分。这些statments可以是任何有效的Ruby语句。例如,我们可以在方法中打印Hello Ruby如下:

class Sample

def hello

puts "Hello Ruby!"

end

end

现在,在下面的例子Sample类创建一个对象,并调用hello方法,看到的结果:

#!/usr/bin/ruby

class Sample

def hello

puts "Hello Ruby!"

end

end

# Now using above class to create objects

object = Sample. new

object.hello

这将产生以下结果:

Hello Ruby!