Often I want to write something that returns "Yes" if true, "No" if false, or "NA" (or any other string really) if nil. Currently I do this:
@contact.boolean ? 'Yes' : (@contact.boolean.nil? ? "NA" : "No")
Is this the shortest way to write this?
Here's one idea:
> {true => "Yes", false => "No", nil => "N/A"}[true]
=> "Yes"
So, of course, you would do {true => "Yes", false => "No", nil => "N/A"}[value]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With