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.
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.
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.
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.
This comment is hidden because it contains spoiler information about the solution
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.Loading more items...