2008年8月29日 星期五

File, Pathname, Dir

http://www.ruby-doc.org/core/classes/File.html
http://www.ruby-doc.org/core/classes/Pathname.html
http://www.ruby-doc.org/core/classes/Dir.html#M002347

join, dirname, __FILE__, RAILS_ROOT, directory?

require File.join(File.dirname(__FILE__), 'boot')

RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)


>> File.directory?("#{RAILS_ROOT}/app")
=> true
>> File.exist?("#{RAILS_ROOT}/app")
=> true

>> File.exist?("#{RAILS_ROOT}/app/controllers/application.rb")
=> true
>> File.directory?("#{RAILS_ROOT}/app/controllers/application.rb")
=> false

Dir is file, too


Dir["config.?"] #=> ["config.h"]
Dir.glob("config.?") #=> ["config.h"]
Dir.glob("*.[a-z][a-z]") #=> ["main.rb"]
Dir.glob("*.[^r]*") #=> ["config.h"]
Dir.glob("*.{rb,h}") #=> ["main.rb", "config.h"]
Dir.glob("*") #=> ["config.h", "main.rb"]
Dir.glob("*", File::FNM_DOTMATCH) #=> [".", "..", "config.h", "main.rb"]

沒有留言: