Fairly new to ruby, playing with Mini Test and log4r, trying to write a wrapper class.
对于ruby来说,这是一种全新的体验,使用迷你测试和log4r,尝试编写一个包装类。
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.
我搜索了相关的文章,以及关于“初始化”参数的web。它们不是直接相关的,或者我误解了它们。任何帮助表示赞赏。
1 个解决方案
#1
3
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.
我不知道你想要做什么,但是需要一个参数。错误数量的参数错误意味着您试图调用一个具有意外数量参数的方法。(2 for 1)表示你试图用一个与两个参数的参数来调用一个方法。
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.
在大多数情况下,需要使用ruby文件的顶部,告诉ruby解释器在加载这个文件之前应该加载其他ruby文件。
require
does not declare gem dependencies, so gem versions are irrelevant here. You can put these in the Gemfile
file.
需要不声明gem依赖,所以gem版本在这里无关。你可以把这些放到Gemfile文件中。
#1
3
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.
我不知道你想要做什么,但是需要一个参数。错误数量的参数错误意味着您试图调用一个具有意外数量参数的方法。(2 for 1)表示你试图用一个与两个参数的参数来调用一个方法。
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.
在大多数情况下,需要使用ruby文件的顶部,告诉ruby解释器在加载这个文件之前应该加载其他ruby文件。
require
does not declare gem dependencies, so gem versions are irrelevant here. You can put these in the Gemfile
file.
需要不声明gem依赖,所以gem版本在这里无关。你可以把这些放到Gemfile文件中。