Ad
  • Custom User Avatar

    Thanks! That's why we're here right? Learning something new.

  • Custom User Avatar

    That's because (in Ruby and many other languages) you can use an integer to get to the n-th charactetr in a String just as you can get the n-th element of an Array.
    You can think of a String as an ordered list of charscters (technically code points, but thats another story).

  • 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

    The plain reason is: Because the tests fail without this, as spaces & a few other characters match the regular expression.

    I didn't know why, when I wrote this solution (and and to think about it for a while, now that you've asked your question. A very good question!

    Now I know: The regex character class contains too many characters. The regex should be written more like this:

    /[#{REPLACEMENTS.keys.join}]/
    

    With this regular expression the method becomes even simpler, because the || match isn't needed anymore.

    That was a really good catch! Thanks for asking – and Happy hacking. 🧑‍💻

  • Custom User Avatar
  • Custom User Avatar

    Brilliant. …
    Barely comprehensible, but brilliant!

  • Custom User Avatar

    Thank you.

    Yes, I did.
    I sometimes pick a technique or approach for a kata first and then use that to solve the problem.
    (This is nothing you'd do in code for production, where you look for the best approachs to solve a problem.)

    Sometimes it's interesting to see how solutions emerge from a given technique.
    In this case it's the way method_missing is used to deal with part of the problem.

  • Custom User Avatar

    Nice!
    The only change I'd make is to replace split('') with chars, and — maybe — call first instead of [0].

  • Custom User Avatar

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

  • Custom User Avatar

    I'd like to add that not adding the 'return' is more idiomatic Ruby (& also following the Ruby style guide, see https://rubystyle.guide/#no-explicit-return).

  • Custom User Avatar
  • Custom User Avatar

    Agreed: Since the description mentions that only valid input will be used and the tests being exhaustive (i.e. checking all months in a year), random tests aren'T needed.

  • Custom User Avatar

    In cases ilke this, I mostly put the parentheses in to remind myself about the precedence of the '' (in partucular).

  • Custom User Avatar

    For the Ruby version the method name should've followed Rubyy's naming convention and be binary_gcd, instead of BinaryGCD (which, given the first character is upper case indicates a constant or class.

  • Custom User Avatar

    Interestingly the solution voted (at the time of this writing) most "Best Practice", won't cut it in Ruby 2.5:

    STDERR
    main.rb:2: warning: constant ::Fixnum is deprecated
    main.rb:66: warning: constant ::Fixnum is deprecated
    

    I believe this is due to the redefinition of a certain method in the mentioned class in the kata's test cases

    I consider this to be an issue, since the tests should work for all language versions the kata accepts.
    Also, I haven't seen this kind of error in any of the other Ruby katas I finished (a bit more than 1500 at this time).

    Cheers

  • Loading more items...