2015-12-24 66 views
0

我是Ruby編程和Rakefile編程的新手。 我已經寫了代碼,但並不如預期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 

當我運行-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

製作報告包含的其他任務命名空間:http://stackoverflow.com/questions/15106414/rake-default-task-and-namespaces#15213296 – jackrabbit

+0

非常感謝您的幫助...我是能夠按預期得到結果 – user1932914

回答

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 
相關問題