Timeline for Expressing multiple AND conditions in ruby
Current License: CC BY-SA 3.0
12 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Nov 12, 2012 at 18:06 | history | edited | tokland | CC BY-SA 3.0 |
added 68 characters in body
|
Nov 10, 2012 at 14:18 | history | edited | tokland | CC BY-SA 3.0 |
edited body
|
Nov 9, 2012 at 14:28 | history | edited | tokland | CC BY-SA 3.0 |
added 181 characters in body
|
Nov 9, 2012 at 14:03 | comment | added | the Tin Man |
When I was writing assembly language, Perl and C, I wrote my conditionals a lot more tersely and concisely. Languages like Ruby encourage a more expressive coding style, that tries to sit between a conversation with the computer, and cold-logic. I prefer conciseness over verbosity personally, but not everyone can deal with Perl-like code, so I try to adjust. In your answer above, your point about short-circuiting is important, especially as the list of conditions grows. Blindly looping over every element can become costly so && and || become important.
|
|
Nov 9, 2012 at 11:32 | comment | added | tokland | @the Tin Man. Thanks for commenting. I think we had our differences in the past about how to write logic, didn't we? :-) I agree that it's hard to get it right, logic is so important (is there anything more important in an app?) that it deserves to invest time on it. For me the best logic is that which resembles mathematical logic, that uses no early "return"s, in-line conditionals, nor any other imperative stuff. Just the good old logic expressions. | |
Nov 9, 2012 at 5:08 | comment | added | the Tin Man |
Sometimes the logic setting up a condition is the worst part of programming, and, after getting done, still won't look right. It's like staring at a word and it just doesn't look like it's spelled right, though you know it is. I generally try to go with whatever combination results in the most concise, yet readable, code for the long-term. After I get done writing it, it could be passed to several other programmers so I don't want them coming back later asking what-the-heck that code does. I've used all? against an array, and chains of && and || , depending on what felt right.
|
|
Nov 5, 2012 at 8:41 | vote | accept | koa | ||
Nov 5, 2012 at 8:02 | comment | added | tokland |
@koa: all? . The enumerable methods are a must read (really, read and understand them one by one). ruby-doc.org/core-1.9.3/Enumerable.html
|
|
Nov 5, 2012 at 8:01 | history | edited | tokland | CC BY-SA 3.0 |
added 1 characters in body
|
Nov 5, 2012 at 7:50 | comment | added | koa | I received an undefined method 'all' on the code example above. See repl.it/D7X | |
Nov 4, 2012 at 21:14 | history | edited | tokland | CC BY-SA 3.0 |
added 110 characters in body
|
Nov 4, 2012 at 19:49 | history | answered | tokland | CC BY-SA 3.0 |