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.
What is
$1
?The challenge description must have changed since I submitted this, 3 months ago.
Probably because the author realised it was a ridiculously trivial problem if you actually follow best practices and use the standard library...
The challenge explicitly forbids using Prime...
dtp
Everyone who has written essay-length solutions... There's no need to reinvent the wheel!!
However, unlike other languages, ruby does not have nested methods. (http://stackoverflow.com/questions/4864191/is-it-possible-to-have-methods-inside-methods)
What you have done here is akin to dynamically redefining
try_decomp
(as a regular instance method, NOT a nested method), every time you calldecompose
.So like I said, this works, but is not "the ruby way". Your code is not actually doing what you think it is doing. This would be like assuming Constants, or Threads, or Truthyness, etc works just like other languages.
Your code may run fine, but one day you'll hit obscure bugs by doing something like this in ruby, unless you actually know what you're doing.
(I repeat: If you really want, at least make it a proc.)
It's not confusing at all. Here, it is the same as the way one would write a nested function in just about any other language.
2 things I don't really like about my own solution here, and would appreciate any suggestions:
A method inside a method? :/
It may be valid syntax, but is unnecessarily confusing! Just make it a separate, private method.
Or if you really want, at least make it a proc.
The string interpolation is unnecessary. You can just do $1.upcase
Sorry, but this is a terrible kata.
You are asking a bad question, teaching bad practices, and have bad tests.
The "correct" answer to this question is simply to do something like:
You should NEVER try to "fully validate" email addresses against a regex. You're not solving any real problems in doing so, and are only going to create ones! And it's also not at all important for your method to return a literal true/false (in ruby).
There is a HUGE range of possible valid email addresses. Your test cases here a not even the tip of the iceberg. For example, the following is a perfectly valid email address: "my weird email"@domain
This kata should be taken down.
Does not work for e.g. string = "\nabc123\n" -- this will return true; should be false.
Also, don't need the "? true : false" part of your method. It's not vital that this method returns a boolean value at all, and even if it was, there is a simpler way to achieve this: !!string.tr("\n", "").match(/^[a-zA-Z\d]+$/)
You don't really need to do "!!", unless you have a specific reason to ensure this method really does return true/false (probably not!). Methods ending in "?" should have "truthy/falsey" return values, bit it's perfectly fine for these to be non-boolean. For example: Numeric#nonzero?, or File#size? do not return true/false.
You should really be using \z, not \Z. \Z allows the inclusion of a newline at the end of the string.