Feature #13260
closedKernel#Boolean
Description
I think we have lots of occasions to receive a true
or false
value as a string input, and want to convert it to true
or false
. Perhaps we can have a method Kernel#Boolean
in a similar spirit to Kernel#Integer
and its kins, which takes an optional keyword argument exception
(similar to https://bugs.ruby-lang.org/issues/12732) and strict
(defaulted to true).
Boolean("true") # => true
Boolean("false") # => false
Boolean("foo") # => ArgumentError
Boolean("foo", exception: nil) # => nil
Boolean("1") # => ArgumentError
Boolean("1", strict: false) # => true
Boolean("yes", strict: false) # => true
Boolean("0", strict: false) # => false
Boolean("no", strict: false) # => false
Boolean("foo", strict: false, exception: nil) # => nil
Updated by sawa (Tsuyoshi Sawada) almost 8 years ago
Please note that this proposal is distinct from any proposal to introduce the Boolean
class into Ruby. In think the latter does not make sense, and I am against it.
Updated by matz (Yukihiro Matsumoto) almost 8 years ago
- Status changed from Open to Feedback
The proposal is still vague, especially when strict: false
. The criteria are totally culture dependent.
"yes" may be true for English speaking people, but what about "はい" (Japanese) or "Ja" (German)? The list would go on.
Until the ambiguity resolved, I am against the proposal.
Matz.
Updated by sawa (Tsuyoshi Sawada) almost 8 years ago
I agree that the proposal was vague. Instead of using strict
, perhaps we can pass a string or strings expressing the language/mode that we want to allow.
Boolean("はい", allow: ["JA", "EN"])
Boolean("0", allow: ["NUMBER"])
Updated by shevegen (Robert A. Heiler) almost 8 years ago
I think we have lots of occasions to receive a true or false value as a string input, and
want to convert it to true or false.
I agree. :)
Not saying that I agree with the API, it feels a bit cumbersome; but I agree with the
comment above.
By the way, I think the first two cases are the one that are the least ambiguous
since they only evaluate the two strings "true" and "false" into the respective
true and false representation:
Boolean("true") # => true
Boolean("false") # => false
(The name "Boolean" is a bit strange though... where does this reside then? In
Kernel? Why not to_bool or to_boolean? Though admittedly the latter also includes
the word "boolean", I guess the uppercased leading part confuses me a bit. I am
aware of Kernel#Integer() or whever it resides, but I think that in general it
may be better to not use too many upcased method names; in ruby we have a lot of
flexibility like .Foo() or class Foo; Foo[] - but anyway, this is a side comment,
as written above I agree with what Tsuyoshi Sawada stated :) )
Updated by matz (Yukihiro Matsumoto) over 7 years ago
- Status changed from Feedback to Rejected
I reject the proposal.
It may be useful for some application, I admit, but it is not the language matter.
Matz.
Updated by hsbt (Hiroshi SHIBATA) about 1 month ago
- Related to Feature #20882: Provide Boolean(...) added