3 ms·
I’m a big fan of Ruby and yes, Ruby handles this elegantly. However, some variation on n%2==0 is idiomatic in a large number of languages, both modern and old
by haswell 5y ago
I’m a big fan of Ruby and yes, Ruby handles this elegantly.
However, some variation on n%2==0 is idiomatic in a large number of languages, both modern and old [0].
This includes Lua, Java, Python, Scala, Perl, PHP, Swift, and many others.
It is true that there are numerous languages with built in convenience methods in their standard libraries, but that does not by itself mean that n%2==0 is problematic and certainly this idiom is deeply entrenched.
There is nothing preventing a developer using those languages from wrapping this in a function, and indeed this is probably a good idea if it’s something you find yourself using repeatedly throughout your code base, just like any other often-repeated code snippets.
- [0] https://www.rosettacode.org/wiki/Even_or_odd https://www.rosettacode.org/wiki/Even_or_odd