-
Notifications
You must be signed in to change notification settings - Fork 21.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TimeWithZone#advance: use #any? instead of #detect
- Loading branch information
Showing
1 changed file
with
1 addition
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5120429
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why any? over detect?
5120429
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nate:
detect
returns the object found,any?
returns a boolean, which is all that’s needed here.5120429
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Enumerations are excessive when there is a clearer way to express what you mean:
(Or change unless to if and reverse the if and else clauses.)
5120429
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Josh: +1
Except that when there’s an
else
clause, I prefer usingif
instead ofunless
. In this case:Even with no
else
clause, I findif foo.any?
to be more obvious thanunless foo.empty?
which I consider a double negative.5120429
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How using a guard clause? Something like:
Or coerce the options into a duration and let the duration decide.
5120429
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
5120429
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
still, +1 on josh
5120429
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
raggi: +1 for
values_at
! Plus, sinceany?
returnstrue
if any element is non-nil, your line can be simplified as (when using theif
form):5120429
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Productive conversation thread. I took the suggestion above, so now this line reads:
because it looked a little cleaner than the block syntax, and read nicely, i.e., “if any values at years, weeks, months, days, then …”
As a nice-to-have, it seems a bit faster — I did a simple benchmark, the results are kind of interesting:
http://pastie.textmate.org/385041
Array#empty? looks to be faster than #any? (at least for this example), but it didn’t seem worth flipping around the logic of the if clause just to chase those extra few cycles.
5120429
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, I like the values_at trick to avoid the call to keys. Thanks for doing the benchmarks.