Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
This comment is hidden because it contains spoiler information about the solution
+1 @viniciuscb
Suggestion: email
info@codewars.com
and/or Tweet to@codewars
and@QualifiedIO
, the creators of Codewars.+1 @SimonSwanky
Suggestion: email
info@codewars.com
and/or Tweet to @codewars and @QualifiedIO, the creators of Codewars.This solution received the most votes for "Best Practices." But you and I agree, this monkey-patching of Array and Numeric is not a good practice, let alone "best."
I didn't claim it would be.
However… if anywhere, I think it's acceptable especially in a narrow use case, such as a coding exercise. THe reason: Precisely because it's a narrow use case, it won't cause harm elsewhere.
That said, using refinements would have been a cleaner way to achieve the effect, without reopening standard classes.
In a real system that deals with geometrical objects, I'd expect that each of these objects would respond to
area
anyway.Numeric
andArray
are classes that represent structures of generic data. It is not good practice to add methods to these classes that are only meaningful in a narrow use case.It's correct to say that the problem does not require
five_num
to check for invalid input.This approach changes the interface of Array, adding a new method that throws a
NoMethodError
when the Array instance doesn't conform to a special use case. As a result, any subsequent instance of Array can respond to a method that may be meaningless (and unsafe) for that instance.Why change the interface of Array?
Or to put it another way, would this also be a good solution? Why or why not?
Then it won't work, but the instructions say:
Create a five_num method that takes an array of integers as an argument
so checking for invalid input is not a requirement.
What if an instance of Array contains non-Numeric values?