From: duerst via ruby-core Date: 2024-11-12T01:13:06+00:00 Subject: [ruby-core:119880] [Ruby master Feature#20882] Provide Boolean(...) 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/activemodel/lib/active_model/type/boolean.rb#L15-L25 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/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/