Ruby类引发ArgumentError:错误的参数数目(1 0)

问题描述:

我试图执行一个rake任务进度条,我抢进度条的代码从下面的网站Ruby类引发ArgumentError:错误的参数数目(1 0)

https://coderwall.com/p/ijr6jq/rake-progress-bar

progress_bar。 RB

class ProgressBar 

    def initialize(total) 
    @total = total 
    @counter = 1 
    end 

    def increment 
    complete = sprintf("%#.2f%", ((@counter.to_f/@total.to_f) * 100)) 
    print "\r\e[0K#{@counter}/#{@total} (#{complete})" 
    @counter += 1 
    end 

end 

progress_bar_test.rake

namespace :progress_bar_test do 

    desc "Testing progress bar" 

    task :start => :environment do 
     items = (1..1000).to_a 
     progress_bar = ProgressBar.new(items.size) 
     items.each do |item| 
     item.to_s ## Call a real method here, example: `item.update(foo: 'bar')` 
     progress_bar.increment 
     end 
    end 
end 

瓦恩我运行rake任务,我得到了以下错误

ArgumentError: wrong number of arguments (1 for 0) 

完整的错误消息

rake aborted! 
ArgumentError: wrong number of arguments (1 for 0) 
/home/user/rails_app/lib/tasks/progress_bar_test.rake:8:in `initialize' 
/home/user/rails_app/lib/tasks/progress_bar_test.rake:8:in `new' 
/home/user/rails_app/lib/tasks/progress_bar_test.rake:8:in `block (2 levels) in <top (required)>' 
Tasks: TOP => progress_bar_test:start 
(See full trace by running task with --trace) 

But when i initialize the same class in IRB i did not face any problem

任何帮助,将不胜感激,在此先感谢

+0

你能否请张贴更多的错误信息? – phoenix

+0

@phoenix用完整的错误信息更新了这个问题 –

您可能遇到的progress_barruby-progressbar安装在您系统上的gem。

当你做require 'progress_bar',它加载的是宝石,而不是你的本地类。你可以尝试做require_relative 'progress_bar',或者重命名你的类(和/或它的文件名),以便加载你的本地文件而不是gem。

+0

您的系统上可能已经安装了['progress_bar' gem](https://github.com/paul/progress_bar)。当你需要'progress_bar'时,它会加载宝石,而不是你的本地类。您可以尝试执行'require_relative'progress_bar'',或重命名您的类(和/或它的文件名),以便加载您的本地文件而不是gem。 –

+0

Ruy Diaz,你有一个鹰眼人LOL,是的,的确,我已经安装了一个宝石Ruby-progressbar这个宝石也有一个类ProgressBar,那就是问题了,哦,你救了我一天的人,谢谢 –