简体   繁体   中英

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

I was trying to implement progress bar for a rake task , i grab the code for progress bar from the below website

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

When i run the rake task i got the following error

ArgumentError: wrong number of arguments (1 for 0)

Full error message

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

Any help would be appreciated, Thanks in advance

You likely have the progress_bar or ruby-progressbar gem installed on your system.

When you do require 'progress_bar' , it's loading the gem and not your local class. You could try doing require_relative 'progress_bar' , or renaming your class (and/or it's file name) so that it loads your local file instead of the gem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM