Ad
  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    +1 @viniciuscb

    Suggestion: email info@codewars.com and/or Tweet to @codewars and @QualifiedIO, the creators of Codewars.

  • Custom User Avatar

    +1 @SimonSwanky

    Suggestion: email info@codewars.com and/or Tweet to @codewars and @QualifiedIO, the creators of Codewars.

  • Custom User Avatar

    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."

  • Custom User Avatar

    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.

  • Custom User Avatar

    Numeric and Array 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.

  • Custom User Avatar

    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?

    class Object
      def median...
      def first_quartile...
      def third_quartile...
    end
    
  • Custom User Avatar

    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.

  • Custom User Avatar

    What if an instance of Array contains non-Numeric values?