我有一个包含文件的目录:
folder/
-spider.rb
-elephant.rb
-monkey.rb
我也有哈希,包含这样的对
rename_filenames =
{ "spider" => "bear",
"elephant" => "rat",
"monkey" => "tiger" }
如何使用哈希值重命名文件?
folder/
-bear.rb
-rat.rb
-tiger.rb
答案 0 :(得分:2)
您可以简单地迭代哈希并相应地重命名:
rename_filenames.each do |old_name, new_name|
File.rename("folder/#{old_name}.rb", "folder/#{new_name}.rb")
end