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.
Thanks! That's why we're here right? Learning something new.
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).
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.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:
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. 🧑💻
Thanks! 🙏🏼
Brilliant. …
Barely comprehensible, but brilliant!
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.Nice!
The only change I'd make is to replace
split('')
withchars
, and — maybe — callfirst
instead of[0]
.This comment is hidden because it contains spoiler information about the solution
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).
Good point. Thanks!
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.
In cases ilke this, I mostly put the parentheses in to remind myself about the precedence of the '' (in partucular).
For the Ruby version the method name should've followed Rubyy's naming convention and be
binary_gcd
, instead ofBinaryGCD
(which, given the first character is upper case indicates a constant or class.Interestingly the solution voted (at the time of this writing) most "Best Practice", won't cut it in Ruby 2.5:
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...