vim-ruby-2007.05.07 bin/install.rb学习 (三)
概要
接着分析代码:
分析代码:
#=======================================
SOURCE_FILES = %w{
autoload/rubycomplete.vim
compiler/eruby.vim
compiler/ruby.vim
compiler/rubyunit.vim
ftdetect/ruby.vim
ftplugin/eruby.vim
ftplugin/ruby.vim
indent/eruby.vim
indent/ruby.vim
syntax/eruby.vim
syntax/ruby.vim
}
class Env
def Env.determine_source_directory
# 1. try the directory above this installation script.
vim_ruby_source_dir = File.expand_path(File.join( File.dirname($0), '..'))
puts $0.class, $0
puts File.dirname($0).class, File.dirname($0)
puts File.join( File.dirname($0), '..').class, File.join( File.dirname($0), '..')
puts vim_ruby_source_dir.class, vim_ruby_source_dir
return vim_ruby_source_dir if _valid_vim_ruby_dir( vim_ruby_source_dir )
# 2.
private_class_method
def Env._valid_vim_ruby_dir(dir)
Dir.chdir(dir) do
return SOURCE_FILES.all? {|path| FileTest.file?(path)}
end
end
end
end
Env.determine_source_directory
#========================================
结果:
String -> $0的类型
D:/soft/ruby/vim-ruby-2007.05.07/vim-ruby-2007.05.07/bin/test.rb -> $0的值,即表示$0代表本身ruby源程序名
String -> File.dirname($0).类型
D:/soft/ruby/vim-ruby-2007.05.07/vim-ruby-2007.05.07/bin -> File.dirname($0)的值,即 $0的绝对路径
String
D:/soft/ruby/vim-ruby-2007.05.07/vim-ruby-2007.05.07/bin/.. -> File.join( File.dirname($0), '..'
String
D:/soft/ruby/vim-ruby-2007.05.07/vim-ruby-2007.05.07 -> File.expand_path的结果,这个以后可能要考察一下
---------------------
Env._valid_vim_ruby_dir(dir)的定义:
1、前面带有private_class_mothod表示是私有成员
2、_valid_vim_ruby_dir前面的"_"是他们的一种编程风格,这个风格很好,看到这样的就知道是私有的
3、里面几个功能都不是很清楚:
Dir.chdir
SOURCE_FILES.all?
FileTest.file?
运行如下代码:
Dir.chdir(dir) do
SOURCE_FILES.each {|path| puts "#{path} : #{FileTest.file?(path)}"}
end
得到的结果是:
autoload/rubycomplete.vim : true
compiler/eruby.vim : true
compiler/ruby.vim : true
compiler/rubyunit.vim : true
ftdetect/ruby.vim : true
ftplugin/eruby.vim : true
ftplugin/ruby.vim : true
indent/eruby.vim : true
indent/ruby.vim : true
syntax/eruby.vim : true
syntax/ruby.vim : true
查看一下fxri
enum.all? [{|obj| block } ] => true or false
-------------------------------------------------------------------------------
Passes each element of the collection to the given block. The method returns true if the block never returns false or nil. If the block is not given, Ruby adds an implicit block of {|obj| obj} (that is all? will return true only if none of the collection members are false or nil.)
%w{ ant bear cat}.all? {|word| word.length >= 3} #=> true
%w{ ant bear cat}.all? {|word| word.length >= 4} #=> false
[ nil, true, 99 ].all? #=> false
嗯,一个模式;传递容器的每个元素到block中,如果其中一个为false,传回false,否则,传回true
FileTest
FileTest implements file test operations similar to those used in File::Stat. It exists as a standalone module, and its methods are also insinuated into the File class. (Note that this is not done by inclusion: the interpreter cheats).
---------------------------------------------------------------- FileTest#file?
File.file?(file_name) => true or false
-------------------------------------------------------------------------------
Returns true if the named file exists and is a regular file.
嗯,FileTest实现了很多查询FILE信息的函数
例如File.file?(file_name) 查询file_name存在且是常规的文件
File::stat(file_name)
返回文件的信息
------------------------------------------------------------- File::expand_path
File.expand_path(file_name [, dir_string] ) -> abs_file_name
-------------------------------------------------------------------------------
Converts a pathname to an absolute pathname.
哦,只要查查fxri,什么都搞定了,就这样。