Best way of returning a random boolean value
I've been using this for some time to return either true
or false
when building fake seed data. Just wondering if anybody has a better, more succinct or verbose way of returning either true
or false
.
rand(2) == 1 ? true : false
Solution 1:
A declarative snippet using Array#sample:
random_boolean = [true, false].sample
Solution 2:
How about removing the ternary operator.
rand(2) == 1