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.
Well I just learned a new method. Very clean solution!
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).
why don't we need to turn em into arrays ?
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.If you have return, it is an explicit return which can be at any point in a method, but the last expression of any method is always returned as an implicit return, so no return is ever needed for the last expression in a Ruby method.
This comment is hidden because it contains spoiler information about the solution
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. 🧑💻
Just out of curiosity, why is
|| match
needed in the #gsub block? It seems to me that you can only match characters fromREPLACEMENTS.keys
and only put single-character keys into that hash.In any case, this gets my vote for "Best Practices".
OP solved it, closing
16 series ~~
Thanks! 🙏🏼
This is easy to read. Kudos.
Loading more items...