[ruby-core:119852] [Ruby master Feature#20882] Provide Boolean(...)

Issue #20882 has been reported by getajobmike (Mike Perham). ---------------------------------------- Feature #20882: Provide Boolean(...) https://bugs.ruby-lang.org/issues/20882 * Author: getajobmike (Mike Perham) * Status: Open ---------------------------------------- Ruby provides Integer(...) and Float(...) global methods to coerce values. Is there a similar method for Booleans? I'd like to do something like: ``` # ENV["SOME_FEATURE"] is unset Boolean(ENV["SOME_FEATURE"]) # => false # ENV["SOME_FEATURE"] is unset, but allow a default? Boolean(ENV["SOME_FEATURE"], true) # => true # explicitly disable ENV["SOME_FEATURE"] = "0" Boolean(ENV["SOME_FEATURE"], true) # => false # explicitly enable ENV["SOME_FEATURE"] = "1" Boolean(ENV["SOME_FEATURE"]) # => true ``` -- https://bugs.ruby-lang.org/

Issue #20882 has been updated by ioquatix (Samuel Williams). I am the current maintainer of the `boolean` gem. You can check the implementation here: https://github.com/ioquatix/boolean I think `Boolean` can be adopted as a core Ruby class, but I think it has been discussed before and rejected because it only has two values. However, for type annotation and type coercion, it can be useful. ---------------------------------------- Feature #20882: Provide Boolean(...) https://bugs.ruby-lang.org/issues/20882#change-110544 * Author: getajobmike (Mike Perham) * Status: Open ---------------------------------------- Ruby provides Integer(...) and Float(...) global methods to coerce values. Is there a similar method for Booleans? I'd like to do something like: ``` # ENV["SOME_FEATURE"] is unset Boolean(ENV["SOME_FEATURE"]) # => false # ENV["SOME_FEATURE"] is unset, but allow a default? Boolean(ENV["SOME_FEATURE"], true) # => true # explicitly disable ENV["SOME_FEATURE"] = "0" Boolean(ENV["SOME_FEATURE"], true) # => false # explicitly enable ENV["SOME_FEATURE"] = "1" Boolean(ENV["SOME_FEATURE"]) # => true ``` -- https://bugs.ruby-lang.org/

Issue #20882 has been updated by bkuhlmann (Brooke Kuhlmann). In case it's of interest, I use my [Refinements](https://alchemists.io/projects/refinements#_to_bool) gem for this. Example: ``` ruby using Refinements::String ENV["SOME_FEATURE"].to_bool # Either `true` or `false` depending on the resolved value. ENV.fetch("SOME_FEATURE", "0").to_bool # `false` ``` ---------------------------------------- Feature #20882: Provide Boolean(...) https://bugs.ruby-lang.org/issues/20882#change-110545 * Author: getajobmike (Mike Perham) * Status: Open ---------------------------------------- Ruby provides Integer(...) and Float(...) global methods to coerce values. Is there a similar method for Booleans? I'd like to do something like: ``` # ENV["SOME_FEATURE"] is unset Boolean(ENV["SOME_FEATURE"]) # => false # ENV["SOME_FEATURE"] is unset, but allow a default? Boolean(ENV["SOME_FEATURE"], true) # => true # explicitly disable ENV["SOME_FEATURE"] = "0" Boolean(ENV["SOME_FEATURE"], true) # => false # explicitly enable ENV["SOME_FEATURE"] = "1" Boolean(ENV["SOME_FEATURE"]) # => true ``` -- https://bugs.ruby-lang.org/

Issue #20882 has been updated by HarlemSquirrel (Kevin McCormack). A project I work on has added the following patch. I kind of the like the simplicity of `#to_b` ``` class Object # Returns true for '1', 'true', true # Returns false for '0', 'false', false, '', nil # # @return [Boolean] def to_boolean present? && ActiveRecord::Type::Boolean.new.cast(self) end alias to_b to_boolean end ``` ---------------------------------------- Feature #20882: Provide Boolean(...) https://bugs.ruby-lang.org/issues/20882#change-110550 * Author: getajobmike (Mike Perham) * Status: Open ---------------------------------------- Ruby provides Integer(...) and Float(...) global methods to coerce values. Is there a similar method for Booleans? I'd like to do something like: ``` # ENV["SOME_FEATURE"] is unset Boolean(ENV["SOME_FEATURE"]) # => false # ENV["SOME_FEATURE"] is unset, but allow a default? Boolean(ENV["SOME_FEATURE"], true) # => true # explicitly disable ENV["SOME_FEATURE"] = "0" Boolean(ENV["SOME_FEATURE"], true) # => false # explicitly enable ENV["SOME_FEATURE"] = "1" Boolean(ENV["SOME_FEATURE"]) # => true ``` -- https://bugs.ruby-lang.org/

Issue #20882 has been updated by murb (Maarten Brouwers). +1 for the request, I don't like to resorting to `ActiveRecord::Type::Boolean.new.cast(value)` to cast booleans. I would prefer a more explicit `.parse` which DateTime has though: ``` DateTime.parse("2021-04-13T14:20") ``` ``` Boolean.parse("0") # false Boolean.parse(1) # true Boolean.parse("t") # true ``` ---------------------------------------- Feature #20882: Provide Boolean(...) https://bugs.ruby-lang.org/issues/20882#change-110552 * Author: getajobmike (Mike Perham) * Status: Open ---------------------------------------- Ruby provides Integer(...) and Float(...) global methods to coerce values. Is there a similar method for Booleans? I'd like to do something like: ``` # ENV["SOME_FEATURE"] is unset Boolean(ENV["SOME_FEATURE"]) # => false # ENV["SOME_FEATURE"] is unset, but allow a default? Boolean(ENV["SOME_FEATURE"], true) # => true # explicitly disable ENV["SOME_FEATURE"] = "0" Boolean(ENV["SOME_FEATURE"], true) # => false # explicitly enable ENV["SOME_FEATURE"] = "1" Boolean(ENV["SOME_FEATURE"]) # => true ``` -- https://bugs.ruby-lang.org/

Issue #20882 has been updated by Earlopain (A S). I'm positive and would like to use something like this as well. However, since `ActiveRecord::Type::Boolean` from Rails has come up, which values should be considered `true`? There are quite a few possible combinations: * 0 and 1 * "0" and "1" * "true" and "false" * "t" and "f" * various other boolean-like words, like "yes"/"no", "on"/"off" * symbols * a variety of different capitalizations of these For reference, here is the list that rails currently considers "false": https://github.com/rails/rails/blob/852d0cd4123463cf215f4b024801b256857295c4... ---------------------------------------- Feature #20882: Provide Boolean(...) https://bugs.ruby-lang.org/issues/20882#change-110555 * Author: getajobmike (Mike Perham) * Status: Open ---------------------------------------- Ruby provides Integer(...) and Float(...) global methods to coerce values. Is there a similar method for Booleans? I'd like to do something like: ``` # ENV["SOME_FEATURE"] is unset Boolean(ENV["SOME_FEATURE"]) # => false # ENV["SOME_FEATURE"] is unset, but allow a default? Boolean(ENV["SOME_FEATURE"], true) # => true # explicitly disable ENV["SOME_FEATURE"] = "0" Boolean(ENV["SOME_FEATURE"], true) # => false # explicitly enable ENV["SOME_FEATURE"] = "1" Boolean(ENV["SOME_FEATURE"]) # => true ``` -- https://bugs.ruby-lang.org/

Issue #20882 has been updated by bunnrf (RF Bunn). This was rejected 2 months ago https://bugs.ruby-lang.org/issues/20756 ---------------------------------------- Feature #20882: Provide Boolean(...) https://bugs.ruby-lang.org/issues/20882#change-110565 * Author: getajobmike (Mike Perham) * Status: Open ---------------------------------------- Ruby provides Integer(...) and Float(...) global methods to coerce values. Is there a similar method for Booleans? I'd like to do something like: ``` # ENV["SOME_FEATURE"] is unset Boolean(ENV["SOME_FEATURE"]) # => false # ENV["SOME_FEATURE"] is unset, but allow a default? Boolean(ENV["SOME_FEATURE"], true) # => true # explicitly disable ENV["SOME_FEATURE"] = "0" Boolean(ENV["SOME_FEATURE"], true) # => false # explicitly enable ENV["SOME_FEATURE"] = "1" Boolean(ENV["SOME_FEATURE"]) # => true ``` -- https://bugs.ruby-lang.org/

Issue #20882 has been updated by austin (Austin Ziegler). bunnrf (RF Bunn) wrote in #note-6:
This was rejected 2 months ago https://bugs.ruby-lang.org/issues/20756
Previously https://bugs.ruby-lang.org/issues/13260
This is not about a Boolean type, but a Boolean coercion function, `Kernel#Boolean` or a casting function, `Object#to_bool`. I support the concept of a coercion function, but think that if string coercion is down, only case-insensitive true/false values should be supported and possibly 0/1 values. Under no circumstances should we make the NOrway mistake that YAML before 1.2 did. (I believe YAML 1.2 supports True, False, TRUE, FALSE, true, and false only.) This would be a good set, IMO. I don’t believe that the Rails / ActiveSupport approach is correct (although supporting symbol versions of the strings would be ok). ---------------------------------------- Feature #20882: Provide Boolean(...) https://bugs.ruby-lang.org/issues/20882#change-110566 * Author: getajobmike (Mike Perham) * Status: Open ---------------------------------------- Ruby provides Integer(...) and Float(...) global methods to coerce values. Is there a similar method for Booleans? I'd like to do something like: ``` # ENV["SOME_FEATURE"] is unset Boolean(ENV["SOME_FEATURE"]) # => false # ENV["SOME_FEATURE"] is unset, but allow a default? Boolean(ENV["SOME_FEATURE"], true) # => true # explicitly disable ENV["SOME_FEATURE"] = "0" Boolean(ENV["SOME_FEATURE"], true) # => false # explicitly enable ENV["SOME_FEATURE"] = "1" Boolean(ENV["SOME_FEATURE"]) # => true ``` -- https://bugs.ruby-lang.org/

Issue #20882 has been updated by Dan0042 (Daniel DeLorme). Earlopain (A S) wrote in #note-5:
There are quite a few possible combinations: * 0 and 1 * "0" and "1" * "true" and "false" * "t" and "f" * various other boolean-like words, like "yes"/"no", "on"/"off"
I would agree with any of these combinations, but not all of them at once. For example ```ruby puts "answer: yes or no?" p Boolean(gets.chomp) ``` This will print "true" even if the answer is "t" rather than "yes", or if the user typo'ed "no" as "on". It seems to me pretty much every variation of this will need to distinguish between 2 values only, and accepting any of 12 preset values will lead to confused logic. "Be liberal in what you accept" but this seems a bit too liberal. ---------------------------------------- Feature #20882: Provide Boolean(...) https://bugs.ruby-lang.org/issues/20882#change-110570 * Author: getajobmike (Mike Perham) * Status: Open ---------------------------------------- Ruby provides Integer(...) and Float(...) global methods to coerce values. Is there a similar method for Booleans? I'd like to do something like: ``` # ENV["SOME_FEATURE"] is unset Boolean(ENV["SOME_FEATURE"]) # => false # ENV["SOME_FEATURE"] is unset, but allow a default? Boolean(ENV["SOME_FEATURE"], true) # => true # explicitly disable ENV["SOME_FEATURE"] = "0" Boolean(ENV["SOME_FEATURE"], true) # => false # explicitly enable ENV["SOME_FEATURE"] = "1" Boolean(ENV["SOME_FEATURE"]) # => true ``` -- https://bugs.ruby-lang.org/

Issue #20882 has been updated by austin (Austin Ziegler). Dan0042 (Daniel DeLorme) wrote in #note-8:
I would agree with any of these combinations, but not all of them at once. For example ```ruby puts "answer: yes or no?" p Boolean(gets.chomp) ``` This will print "true" even if the answer is "t" rather than "yes", or if the user typo'ed "no" as "on". It seems to me pretty much every variation of this will need to distinguish between 2 values only, and accepting any of 12 preset values will lead to confused logic. "Be liberal in what you accept" but this seems a bit too liberal.
GitHub Actions takes what I think is the right choice for this: ```typescript /** * Gets the input value of the boolean type in the YAML 1.2 "core schema" specification. * Support boolean input list: `true | True | TRUE | false | False | FALSE` . * The return value is also in boolean type. * ref: https://yaml.org/spec/1.2/spec.html#id2804923 * * @param name name of the input to get * @param options optional. See InputOptions. * @returns boolean */ export function getBooleanInput(name: string, options?: InputOptions): boolean { const trueValue = ['true', 'True', 'TRUE'] const falseValue = ['false', 'False', 'FALSE'] const val = getInput(name, options) if (trueValue.includes(val)) return true if (falseValue.includes(val)) return false throw new TypeError( `Input does not meet YAML 1.2 "Core Schema" specification: ${name}\n` + `Support boolean input list: \`true | True | TRUE | false | False | FALSE\`` ) } ``` In the main cases where this is needed, one is pulling from an environment variable (which is always a string) or similar input. `Kernel#Integer` throws an exception if the value does not match the expectation of an integer (it is fairly *liberal* with what makes an integer, accepting decimal, binary, hex, and octal inputs). There is no reason that an equivalent `Kernel#Boolean` could not do the same and do so with exactly the six values listed above, *possibly* twelve if we wanted to accept symbol versions as well. I would argue that any other interpretation of a "boolean string" should be handled explicitly by calling `#==` or `#===` (via case) or with pattern matching because otherwise you are opening yourself to invalid or unexpected inputs, which are ultimately security risks. ---------------------------------------- Feature #20882: Provide Boolean(...) https://bugs.ruby-lang.org/issues/20882#change-110571 * Author: getajobmike (Mike Perham) * Status: Open ---------------------------------------- Ruby provides Integer(...) and Float(...) global methods to coerce values. Is there a similar method for Booleans? I'd like to do something like: ``` # ENV["SOME_FEATURE"] is unset Boolean(ENV["SOME_FEATURE"]) # => false # ENV["SOME_FEATURE"] is unset, but allow a default? Boolean(ENV["SOME_FEATURE"], true) # => true # explicitly disable ENV["SOME_FEATURE"] = "0" Boolean(ENV["SOME_FEATURE"], true) # => false # explicitly enable ENV["SOME_FEATURE"] = "1" Boolean(ENV["SOME_FEATURE"]) # => true ``` -- https://bugs.ruby-lang.org/

Issue #20882 has been updated by duerst (Martin Dürst). Earlopain (A S) wrote in #note-5:
I'm positive and would like to use something like this as well. However, since `ActiveRecord::Type::Boolean` from Rails has come up, which values should be considered `true`?
There are quite a few possible combinations: * 0 and 1 * "0" and "1" * "true" and "false" * "t" and "f" * various other boolean-like words, like "yes"/"no", "on"/"off" * symbols * a variety of different capitalizations of these
For reference, here is the list that rails currently considers "false": https://github.com/rails/rails/blob/852d0cd4123463cf215f4b024801b256857295c4...
I think we shouldn't mix program-internal conversions and human-language choices. Anything like the following (very rough/quick translations) will not work: ```ruby puts "Antwort: Ja oder nein?" # German p Boolean(gets.chomp) puts "Reponse: Si ou non?" # French p Boolean(gets.chomp) puts "解答: はいまたはいいえ?" # Japanese p Boolean(gets.chomp) ``` ---------------------------------------- Feature #20882: Provide Boolean(...) https://bugs.ruby-lang.org/issues/20882#change-110572 * Author: getajobmike (Mike Perham) * Status: Open ---------------------------------------- Ruby provides Integer(...) and Float(...) global methods to coerce values. Is there a similar method for Booleans? I'd like to do something like: ``` # ENV["SOME_FEATURE"] is unset Boolean(ENV["SOME_FEATURE"]) # => false # ENV["SOME_FEATURE"] is unset, but allow a default? Boolean(ENV["SOME_FEATURE"], true) # => true # explicitly disable ENV["SOME_FEATURE"] = "0" Boolean(ENV["SOME_FEATURE"], true) # => false # explicitly enable ENV["SOME_FEATURE"] = "1" Boolean(ENV["SOME_FEATURE"]) # => true ``` -- https://bugs.ruby-lang.org/

Issue #20882 has been updated by matz (Yukihiro Matsumoto). Status changed from Open to Rejected I don't think it works well in the Ruby ecosystem. Rejected (as previous proposals). Matz. ---------------------------------------- Feature #20882: Provide Boolean(...) https://bugs.ruby-lang.org/issues/20882#change-111005 * Author: getajobmike (Mike Perham) * Status: Rejected ---------------------------------------- Ruby provides Integer(...) and Float(...) global methods to coerce values. Is there a similar method for Booleans? I'd like to do something like: ``` # ENV["SOME_FEATURE"] is unset Boolean(ENV["SOME_FEATURE"]) # => false # ENV["SOME_FEATURE"] is unset, but allow a default? Boolean(ENV["SOME_FEATURE"], true) # => true # explicitly disable ENV["SOME_FEATURE"] = "0" Boolean(ENV["SOME_FEATURE"], true) # => false # explicitly enable ENV["SOME_FEATURE"] = "1" Boolean(ENV["SOME_FEATURE"]) # => true ``` -- https://bugs.ruby-lang.org/
participants (11)
-
austin (Austin Ziegler)
-
bkuhlmann (Brooke Kuhlmann)
-
bunnrf (RF Bunn)
-
Dan0042 (Daniel DeLorme)
-
duerst
-
Earlopain (A S)
-
getajobmike (Mike Perham)
-
HarlemSquirrel (Kevin McCormack)
-
ioquatix (Samuel Williams)
-
matz (Yukihiro Matsumoto)
-
murb (Maarten Brouwers)