ruby:当总和为 21 时添加数字并打印 true [英] ruby: adding numbers and printing true when sums are 21
问题描述
这是 rubeque.com 上给出的一个简单问题:编写一个方法,该方法接受任意数量的整数并将它们相加,如果总和为 21,则返回 true.否则为 False.它测试输入:
assert_equal_one?(3, 4, 5, 6, 3), trueassert_equal 二十一?(3, 11, 10), false
这是我目前所拥有的:
def二十一?(*nums)nums.inject(&:+)结尾如果二十一?== 21把真别的错误的结尾
但我收到错误消息:
RuntimeError: 值 '21' 不等于 'true'.
我真的很困惑如何解决这个问题.是否可以将 if/else 语句放在方法中?对不起,如果这个问题真的很基础.我是编程新手.
你需要把你的方法写成
def二十一?(*nums)nums.inject(&:+) == 21结尾
这是一个小演示:-
需要minitest/autorun"类数字def二十一?(*nums)nums.inject(&:+) == 21结尾结尾类 TestNumerics <小测试::测试定义设置@numeric = Numerics.new结尾定义拆解@数字 = 零结尾def test_twenty_one?assert_equal @numeric.twenty_one?(3, 4, 5, 6, 3), trueassert_equal @numeric.twenty_one?(3, 11, 10), 假结尾结尾
让我们运行测试:-
[arup@Ruby]$ ruby test/test_numerics.rb运行选项:--seed 61602# 跑步:.完成时间为 0.001332 秒、750.9402 次运行/秒、1501.8804 次断言/秒.1 次运行,2 次断言,0 次失败,0 次错误,0 次跳过[arup@Ruby]$
在您的方法中,它返回了 Fixnum
实例 21
,您试图将其与 true
进行比较.这就是你得到错误的原因.如果您查看 assert_equal
,你会找到同一个类的实例的2个对象之间的比较,否则会抛出你得到的错误.>
注意:你当然可以把这个nums.inject(&:+)
写成nums.inject(:+)
也因为 Ruby 允许在 #reduce/#inject
方法的情况下这种自由,特别是开箱即用.
更新
卡尔斯Jove Buxeda 提出了一个很好的想法来设计这个问题.这个想法是将方法放在模块中,然后包含它来测试它的方法:
需要minitest/autorun"模块数字def二十一?(*nums)nums.inject(:+) == 21结尾结尾类 TestNumerics <小测试::测试包括数字def test_twenty_one?assert_equal 二十一?(3, 4, 5, 6, 3), trueassert_equal 二十一?(3, 11, 10), false结尾结尾
现在如果我运行它:
arup_ruby$ ruby test/test_numerics.rb运行选项:--seed 1223# 跑步:.完成时间为 0.001067 秒、937.2071 次运行/秒、1874.4142 次断言/秒.1 次运行,2 次断言,0 次失败,0 次错误,0 次跳过arup_ruby$
非常酷的想法!
It's a simple problem given on rubeque.com: write a method that takes any number of integers and adds them to return true if the sum is 21. False otherwise. It tests the input with:
assert_equal twenty_one?(3, 4, 5, 6, 3), true
assert_equal twenty_one?(3, 11, 10), false
Here's what I have so far:
def twenty_one?(*nums)
nums.inject(&:+)
end
if twenty_one? == 21
puts true
else
false
end
But I get the error message:
RuntimeError: The value '21' does not equal 'true'.
I'm really confused on how to fix this. Is it possible to put the if/else statement within the method? And sorry if this question is really basic. I'm new to programming.
You need to write your method as
def twenty_one?(*nums)
nums.inject(&:+) == 21
end
Here is one little demo :-
require "minitest/autorun"
class Numerics
def twenty_one?(*nums)
nums.inject(&:+) == 21
end
end
class TestNumerics < MiniTest::Test
def setup
@numeric = Numerics.new
end
def teardown
@numeric = nil
end
def test_twenty_one?
assert_equal @numeric.twenty_one?(3, 4, 5, 6, 3), true
assert_equal @numeric.twenty_one?(3, 11, 10), false
end
end
Let's run the tests :-
[arup@Ruby]$ ruby test/test_numerics.rb
Run options: --seed 61602
# Running:
.
Finished in 0.001332s, 750.9402 runs/s, 1501.8804 assertions/s.
1 runs, 2 assertions, 0 failures, 0 errors, 0 skips
[arup@Ruby]$
In your method, it was returning Fixnum
instance 21
, which you were trying to compare with true
. That's why you got the error. If you look at the source of assert_equal
, you will find the comparisons between 2 objects which are the instances of the same class, otherwise it will throw the error you got.
Note: You of-course can write this nums.inject(&:+)
as nums.inject(:+)
too as Ruby allows this freedom in case of #reduce/#inject
method particularly out of the box.
Update
Carles Jove Buxeda gave one nice idea to design this problem. The idea is to put the methods inside the module, and then include it to test its methods:
require "minitest/autorun"
module Numerics
def twenty_one?(*nums)
nums.inject(:+) == 21
end
end
class TestNumerics < MiniTest::Test
include Numerics
def test_twenty_one?
assert_equal twenty_one?(3, 4, 5, 6, 3), true
assert_equal twenty_one?(3, 11, 10), false
end
end
Now if I run it :
arup_ruby$ ruby test/test_numerics.rb
Run options: --seed 1223
# Running:
.
Finished in 0.001067s, 937.2071 runs/s, 1874.4142 assertions/s.
1 runs, 2 assertions, 0 failures, 0 errors, 0 skips
arup_ruby$
Very cool idea!
这篇关于ruby:当总和为 21 时添加数字并打印 true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!