Created
May 17, 2010 15:38
-
-
Save louis-wu/403891 to your computer and use it in GitHub Desktop.
File and Dir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#当你把照相机接入电脑时,照片文件夹为F盘。下面的程序对照片进行批量重命名。 | |
pic_names = Dir['F:/**/*.{JPG,jpg}'] | |
puts 'What would you like to call this batch?' | |
batch_name = gets.chomp | |
puts | |
print "Downloading #{pic_names.length} files: " | |
pic_number = 1 #这是计数器,我们从1开始计数 | |
pic_names.each do |name| | |
print '.' # 这是进度条. | |
new_name = if pic_number < 10 | |
"batch_name0#{pic_number}.jpg" | |
else | |
"batch_name#{pic_number}.jpg" | |
end | |
File.rename name, new_name | |
pic_number = pic_number + 1 #计数器增量 | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#搜索当前文件夹中所有后缀为.txt的文件 | |
>> Dir['*.txt'] | |
=> ["DatasGadgets.txt", "JiaQing.txt", "mybook.txt", "myfriends.txt"] | |
#搜索当前文件夹中所有后缀为.txt或.yaml的文件 | |
>> Dir['*.{txt,yaml}'] | |
=> ["DatasGadgets.txt", "JiaQing.txt", "mybook.txt", "myfriends.txt", "tree.yaml"] | |
#搜索当前文件夹及其所有子文件夹中所有后缀为.txt的文件 | |
>> Dir['**/*.txt'] | |
=> ["DatasGadgets.txt", "JiaQing.txt", "louis/jquery/chapter10/buttons/options.txt", "louis/jquery/chapter4/dvds/movies.txt",...] | |
#pwd和chdir的方法 | |
>> Dir.pwd | |
=> "/home/Louis" | |
>> Dir.chdir 'louis' | |
=> 0 | |
>> Dir.pwd | |
=> "/home/Louis/louis" | |
>> Dir.chdir '..' #返回上一级文件夹 | |
=> 0 | |
>> Dir.pwd | |
=> "/home/Louis" | |
#把chdir和Dir[]一起使用。 | |
>> Dir.chdir 'louis/note' do | |
?> puts Dir['*.txt'] | |
>> end | |
e-textediter的使用.txt | |
E-TextEditor快捷键.txt | |
gem_log.txt | |
git.txt | |
programming_art.txt | |
yuming.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#File.open is how you open a file. It creates the file object, | |
#calls it f , runs all the code until it gets to the end, and then closes the file. | |
#save and load | |
>> filename = 'mybook.txt' | |
=> "mybook.txt" | |
>> test_string = 'This is my ruby book: ' + 'Programming ruby' | |
=> "This is my ruby book: Programming ruby" | |
#‘w'表示写入文件 | |
>> File.open filename, 'w' do |f| | |
?> f.write test_string | |
>> end | |
#读取文件内容 | |
>> read_string = File.read filename | |
=> "This is my ruby book: Programming ruby" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'fileutils' | |
# copy config files | |
Dir['config/examples/*'].each do |source| | |
destination = "config/#{File.basename(source)}" | |
unless File.exist? destination | |
FileUtils.cp(source, destination) | |
puts "Generated #{destination}" | |
end | |
end | |
# run rake setup tasks | |
system "rake gems:install db:create:all" | |
system "rake db:migrate --trace" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Well, that’s all well and good if all you want to save and load are single strings. But what if you wanted to save an array of strings? Or an array of integers and floats? 这就需要YAML了