Created
June 9, 2012 05:58
-
-
Save tiegz/2899696 to your computer and use it in GitHub Desktop.
Using the method Array#one? in ruby
This file contains hidden or 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
# The method Array#one? in ruby is intended to be used with a block. It does *not* signify that | |
# there is one element in the array: | |
1.8.7 :066 > [1,2,3].one? | |
=> false | |
1.8.7 :067 > [1,nil,nil].one? | |
=> true | |
# Also, it is slower than just checking the array's size: | |
1.8.7 :047 > Benchmark.ms { 10000.times { [1,2,3].size == 1 } } | |
=> 7.34710693359375 | |
1.8.7 :055 > Benchmark.ms { 10000.times { [1,2,3].one? } } | |
=> 13.7670040130615 |
This file contains hidden or 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
# The method Array#one? in ruby is intended to be used with a block. It does *not* signify that | |
# there is one element in the array: | |
1.8.7 :066 > [1,2,3].one? | |
=> false | |
1.8.7 :067 > [1,nil,nil].one? | |
=> true | |
# Also, it is slower than just checking the array's size: | |
1.8.7 :047 > Benchmark.ms { 10000.times { [1,2,3].size == 1 } } | |
=> 7.34710693359375 | |
1.8.7 :055 > Benchmark.ms { 10000.times { [1,2,3].one? } } | |
=> 13.7670040130615 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment