【发布时间】:2012-04-06 11:47:45
【问题描述】:
我有一个 rake 任务,可以重命名各种文件中的一堆东西。所以,如果有人在 cmd 终端写这个
rename:namechange[funk]
下面的代码应该执行必要的查找/替换。我的问题是我无法让这条线工作。
args[:new_name.capitalize]
知道为什么吗?
namespace :rename do
desc 'changes the name of the app'
task :changename, :new_name do |task, args|
args[:new_name.capitalize]
# change any instances of the term "framework" to the new name of the app
#for testing, just change these: file_names = ['config/environment.rb'] #['config/environments/test.rb', 'config/environments/production.rb', 'config/environment.rb']
file_names = ['app/helpers/application_helper.rb', 'app/views/pages/home.html.erb', 'rakefile', 'config/application.rb', 'config.ru', 'config/database.yml',
'config/environments/development.rb', 'config/environments/test.rb', 'config/environments/production.rb',
'config/environment.rb', 'config/initializers/secret_token.rb', 'config/initializers/session_store.rb', 'config/routes.rb',
'spec/controllers/pages_controller_spec.rb']
file_names.each do |file_name|
text = File.read(file_name)
File.open(file_name, "w") { |file| file << text.gsub("Framework", args[:new_name]) }
end
end
end
更新:我在使用“.capitalize”时遇到了问题,因为它会自动将任何字符放在第一个字符之后作为小写字母。最终,我得出了这个结论:
args[:new_name][0] = args[:new_name].capitalize[0]
【问题讨论】:
-
你想用这个达到什么目的?
-
我编辑了你的标题。这是成人和专业人士的地方。
-
没问题。谢谢你的改变。
标签: ruby-on-rails rake