Rakefile中的一些修改

时间:2015-12-24 05:49:09

标签: ruby rakefile

我是Ruby编程和Rakefile编程的新手。 我写了一段代码,但答案并不像预期的那样

require './database'

desc "Show number of registered users"
task :report_registered_users do
   arr=Database.new.registered_users
   printf("Month     |   No. Of Users\n")
   printf("----------+----------------\n")
   arr.each do |row|
       printf("%s   |  %s\n",row[0],row[1])
   end
end


desc "Show number of registered devices"
task :report_registered_devices do
   arr=Database.new.registered_devices
   printf("Month     |   No. Of devices\n")
   printf("----------+----------------\n")
   arr.each do |row|
       printf("%s   |  %s\n",row[0],row[1])
   end
end

desc "Show number of unregistered users"
task :report_unregistered_users do
    arr=Database.new.unregistered_users
    printf("Month     |   No. Of Users\n")
    printf("----------+----------------\n")
    arr.each do |row|
        printf("%s   |  %s\n",row[0],row[1])
    end
end

desc "Show number of unregistered devices"
task :report_unregistered_devices do
   arr=Database.new.unregistered_devices
   printf("Month     |   No. Of devices\n")
   printf("----------+----------------\n")
   arr.each do |row|
       printf("%s   |  %s\n",row[0],row[1])
   end
end

desc "Show device per user"
task :report_device_per_user do
   arr=Database.new.device_per_user
   printf("No.of Devices     |   No. Of Users\n")
   printf("----------+----------------\n")
   arr.each do |row|
       printf("%s                  |  %s\n",row[0],row[1])
   end
end

运行代码的过程是:

rake -T
rake report_registered_users

当我运行rate -T命令时,输出如下:

rake report_device_per_user         #Show device per user
rake report_registered_devices      #Show number of registered devices
rake report_registered_users        #Show number of registered users
rake report_unregistered_devices    #Show number of unregistered devices
rake report_unregistered_user       #Show number of unregistered user

但我希望输出像这样

rake report:device_per_user         #Show device per user
rake report:registered_devices      #Show number of registered devices
rake report:registered_users        #Show number of registered users
rake report:unregistered_devices    #Show number of unregistered devices
rake report:unregistered_user       #Show number of unregistered user

注意,输出的变化很小 report_device_per_user ---->报告:device_per_user

有人可以帮我解决这个问题吗...提前谢谢 这可能是非常基本但我无法做到这一点

1 个答案:

答案 0 :(得分:1)

您需要命名任务

所以这......

require './database'

desc "Show number of registered users"
task :report_registered_users do
  ...
end

会变成这个......

require './database'

namespace :report do
  desc "Show number of registered users"
  task :registered_users do
    ...
  end
end