【问题标题】:Why does ruby stop me from capitalizing an argument in a rake task? [closed]为什么 ruby​​ 会阻止我在 rake 任务中大写参数? [关闭]
【发布时间】: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


【解决方案1】:

你的意思是写其中之一吗:

args[:new_name].capitalize!
args[:new_name] = args[:new_name].capitalize

【讨论】:

    【解决方案2】:
    args[:new_name.capitalize]
    

    将只在返回 :New_name 的 :new_name 符号上调用大写,并将其用作 args 哈希中的键。你想用

    args[:new_name].capitalize!
    

    这将访问 args 哈希中的 :new_name 并将其大写

    【讨论】:

    猜你喜欢
    • 2014-09-01
    • 2012-06-21
    • 1970-01-01
    • 2023-02-07
    • 2012-03-30
    • 2011-02-10
    • 2021-03-13
    • 1970-01-01
    • 2011-04-03
    相关资源
    最近更新 更多