Ruby class ArgumentError: wrong number of arguments (1 for 0)

Ruby class ArgumentError: wrong number of arguments (1 for 0)

我正在尝试为 rake 任务实现进度条,我从以下网站获取了进度条的代码

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

任何帮助将不胜感激,提前致谢

您的系统上可能安装了 progress_bar or ruby-progressbar gem。

当您执行 require 'progress_bar' 时,它正在加载 gem 而不是您的本地 class。您可以尝试执行 require_relative 'progress_bar',或重命名您的 class(and/or 它是文件名),以便它加载您的本地文件而不是 gem。