开发者

Determining "Truthiness" of an expression? [duplicate]

开发者 https://www.devze.com 2023-02-09 10:11 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicates: Check if Ruby object is a Boolean
This question already has answers here: Closed 12 years ago.

Possible Duplicates:

Check if Ruby object is a Boolean

How can I avoid truthiness in Ruby?

Given an array like the following (for example):

[3, false, "String", 14, "20-31", true, true, "Other String"]

Is there an easier way to determine which elements are actual boolean values without resorting to this, for exa开发者_运维技巧mple?

value === TrueClass || value === FalseClass

Relying on the position in the array is not an option, as it will vary from case-to-case.


You could try (true & value) == value. The part in parentheses seems to always return a boolean; if the value wasn't originally a bool, then it won't be equal to the result. A bool, however, will.

0

精彩评论

暂无评论...
验证码 换一张
取 消