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
Here is one little demo :-
Let's run the tests :-
In your method, it was returning
Fixnum
instance21
, which you were trying to compare withtrue
. That's why you got the error. If you look at the source ofassert_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(&:+)
asnums.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:
Now if I run it :
Very cool idea!
Arup's answer is great. I would suggest to make it a little more generic.
I am really bad with names. This made me think it would be pretty cool to combine your method name with metaprogramming and the gem humanize. You could then have dynamic method names and infer from the method name what value the sum should add up to. So you could have any comparison, like
MyAwesomeGem.three_thousand_and_twenty_four?(3000, 20, 4)
Would be fun to build!
:)