ruby文件夹操作

Python012

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

这并不是一件容易的事情。

在编译体系中,高级语言代码(如C语言)被编译成汇编语言,再被编译成机器代码(0101)才得以被执行。

一个执行c语言代码的工作,需要完成整个编译器体系的工作,所以这并不是解决你这个问题的优秀解决方法。

事实上,你的问题并不是只有你一个人遇到,有很多的人遇到过相似的问题,有些大神则做到了这一件事情,你可以利用他们已经做好的工具来完成你的AVG game。

可以向你推荐一个软件:THE NVL Maker

我并未使用过这个软件做过任何成品游戏,只是帮助朋友了解过这个软件,这是一个功能强大的制作AVG的软件,LZ可以体验一下,也可以寻找更好的软件,LZ也可以分享给大家。

也有一些游戏采用一些脚本语言如(python, ruby)等语言作为代码文件,但是“几乎没有”采用底层语言如C/C++作为代码文件的情况。(有的话求科普)

预祝LZ可以顺利的完成自己的游戏~~~~~

————————————————————————————————————————

以上,错字谅解。

关于 RubyMotion 我已经写过很多文章了,但如何混用Objective-C与Ruby还从未涉及到。实际上你能在RubyMotion项目中使用Objective-C代码,也可以在传统Objective-C的App中使用Ruby代码。也许你一次听来觉得像黑魔法一样,所以来一起看看下面这些示例。

Objective-C in Ruby

很多iOS开发者在现有的Objecive-C代码中存在大量深层的备份日志,这样就为项目手动转换为 RubyMotion 带来了很大的痛苦。然而幸运的是现在仅仅只需要将编译好的Objective-C代码添加到我们的新RubyMotion应用里。

比如我们要在 RubyMotion 应用中使用 Objective-C 形式的 CYAlert 类:

// CYAlert.h

#import <UIKit/UIKit.h>@interface CYAlert : NSObject

+ (void)show@end

// CYAlert.m

#import "CYAlert.h"

@implementation CYAlert

+ (void)show {

UIAlertView *alert = [[UIAlertView alloc] init]

alert.title = @"This is Objective-C"

alert.message = @"Mixing and matching!"

[alert addButtonWithTitle:@"OK"]

[alert show]

[alert release]

}@end

为了在RubyMotion应用中能正常使用,这里需要将CYAlert的两个文件都放置到类似 ./vendor/CYAlert 里面,然后在 Rakefile: 中告诉 RubyMotion 使用vendor_project创建那个文件夹。

Motion::Project::App.setup do |app|

# Use `rake config' to see complete project settings.

app.name = 'MixingExample'

# ./vendor/CYAlert contains CYAlert.h and CYAlert.m

app.vendor_project('vendor/CYAlert', :static)

end

那么现在在 AppDelegate 中,我们这样使用 CYAlert:

class AppDelegate

def application(application, didFinishLaunchingWithOptions:launchOptions)

CYAlert.show

true

end

end