ArgumentError: wrong number of arguments (2 for 1) for 'initialize'?

jbobbylopez picture jbobbylopez · May 6, 2014 · Viewed 8.7k times · Source

Fairly new to ruby, playing with Mini Test and log4r, trying to write a wrapper class.

Getting the following error:

$ ruby logger.rb
Run options: --seed 4605

# Running tests:

E

Finished tests in 0.000000s, Inf tests/s, NaN assertions/s.

  1) Error:
test_debug_messages(TestLogger):
ArgumentError: wrong number of arguments (2 for 1)
    /home/jamlopez/scripts/Yatra.Next/rpm_framework/lib/rpm/core/logger.rb:5:in `initialize'
    logger.rb:6:in `new'
    logger.rb:6:in `setup'

1 tests, 0 assertions, 0 failures, 1 errors, 0 skips

Here's the class:

class RPMLogger
    def initialize
        require 'log4r', :version=>'=1.1.10'
        @log = Logger.new 'log'
        @log.outputters = Outputter.stdout
        @logfile_location = brpm_home
        @timenow = eval( Time.now.utc.iso8601 )
    end
    def debug( msg )
        @log.debug ( '[DEBUG] ' + @timenow + " #{msg}" )
    end
    def info( msg )
        @log.info ( '[INFO] ' + @timenow + " #{msg}" )
    end
    def warn( msg )
        @log.warn ( '[WARNING] ' + @timenow + " #{msg}" )
    end
    def error( msg )
        @log.error ( '[ERROR] ' + @timenow + " #{msg}" )
    end
    def fatal( msg )
        @log.fatal ( '[FATAL] ' + @timenow + " #{msg}" )
    end
end

And here's the test:

require 'minitest/autorun'
require_relative "./../../../lib/rpm/core/logger"

class TestLogger < MiniTest::Unit::TestCase
  def setup
    @logger = RPMLogger.new
    @test_msg = "This is a test log message!"
  end

  def test_debug_messages
    log = @logger.debug( @test_msg )
    assert_match "/^[DEBUG] /", log, msg=nil
  end

end

I'll be the first to concede there are probably several errors in both files (as I'm still learning) but I'll try to take it one step at a time.

I've searched for related SO articles, and on the web regarding arguments to 'initialize'. Either they are not directly related, or I misunderstand them. Any assistance appreciated.

Answer

Uri Agassi picture Uri Agassi · May 6, 2014

Your error is in this line:

require 'log4r', :version=>'=1.1.10'

I'm not sure what you tried to do, but require receives a single argument. The error wrong number of arguments means that you are trying to call a method with an unexpected number of arguments. (2 for 1) means you are trying to call a method with one argument with two.

The only method call in initialize you are calling with two arguments is require - so this is the method in question.

require usage is in most cases at the top of ruby files, telling the ruby interpreter which other ruby files should be loaded before loading this file.

require does not declare gem dependencies, so gem versions are irrelevant here. You can put these in the Gemfile file.