![](/img/trans.png)
[英]Ruby - ArgumentError: wrong number of arguments (given 3, expected 2)
[英]ArgumentError: wrong number of arguments (given 0, expected 1) - Ruby
我有一個測試套件(hello_world_test.rb),它具有以下內容
#!/usr/bin/env ruby
begin
gem 'minitest', '>= 5.0.0'
require 'minitest/autorun'
require_relative 'hello_world'
rescue Gem::LoadError => e
puts "\nMissing Dependency:\n#{e.backtrace.first} #{e.message}"
puts 'Minitest 5.0 gem must be installed for the xRuby track.'
rescue LoadError => e
puts "\nError:\n#{e.backtrace.first} #{e.message}"
puts DATA.read
exit 1
end
# Test data version:
# deb225e Implement canonical dataset for scrabble-score problem (#255)
class HelloWorldTest < Minitest::Test
def test_no_name
assert_equal 'Hello, World!', HelloWorld.hello()
end
def test_sample_name
assert_equal 'Hello, Alice!', HelloWorld.hello('Alice')
end
def test_other_sample_name
assert_equal 'Hello, Bob!', HelloWorld.hello('Bob')
end
end
__END__
在此基礎上,我實現了如下代碼
class HelloWorld
def self.hello
"Hello, World!"
end
def self.hello(named)
"Hello, #{named}!"
end
end
現在我的兩個測試用例正在通過,第一個測試用例正在返回
ArgumentError:參數數量錯誤(給定 0,預期為 1)/Users/vijay/Codes/Ruby/ruby/hello-world/hello_world.rb:7:in
hello' hello_world_test.rb:21:in
test_no_name'
一些機構可以幫助我嗎?
Ruby 中沒有通過 params 重載的方法。 用你對hello
的第二個定義覆蓋了第一個定義。 無論如何,您只需使用一種具有參數默認值的方法即可獲得相同的行為。
def self.hello(named = 'World')
"Hello, #{named}!"
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.