4 ms·
if you are a Ruby programmer cyberferret explicitly said it's a good idea to avoid shorthand methods because new programmers don't understand them, and you're
by onion2k 9y ago
if you are a Ruby programmer
cyberferret explicitly said it's a good idea to avoid shorthand methods because new programmers don't understand them, and you're making the argument that they're OK because experienced programmers will understand them. The essence of your argument is that if you've used the language enough to know what the or-equals statement means then you'll know what it means. I'm not convinced that's a good reason.
- vortico 9y agoFor that specific example though, it's so common in the colloquial Ruby language that all normal Ruby programmers should learn and use it. If you're just taking about readability, the time saved skimming during your career is worth the time spent learning the idiom once in your life.
- joshumax 9y agoCyberferrit said new programmers to the project, not specifically new to the language or programming itself. Working on emulation software I commonly run across snippets such as `res = !!iRegisterVal` which results in some C newbies scratching their heads as to why a useless double negation would be there. But for more seasoned developers its intent can pretty easily recognized...
- CrystalLangUser 9y agoWhy is !! useful? Asking as a ruby programmer.
- asherkin 9y agoIt is a shorthand method of casting to a boolean value - C didn't have a true bool type until "recently" (but did have the concept of truthiness, where true is `is not 0`), so many projects and libraries use `#define TRUE 1`, and #define FALSE 0` or some similar variant. So rather than write `foo ? TRUE : FALSE` or being more explicit `(foo != 0) ? 1 : 0`, you can just do `!!foo`.
- sethrin 9y agoIt's a quick cast to a boolean.
- drusepth 9y agoBecause Ruby doesn't have a to_boolean method. !! is used to convert "truthy" or "falsy" values [0] into true boolean true or false values. For example, def user_signed_in? !!current_user end This returns "true" or "false", instead of a (truthy) User object or a (falsy) nil. [0] https://gist.github.com/jfarmer/2647362 https://gist.github.com/jfarmer/2647362