From: "Earlopain (A S) via ruby-core" Date: 2024-11-09T12:34:05+00:00 Subject: [ruby-core:119865] [Ruby master Feature#20882] Provide Boolean(...) 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/activemodel/lib/active_model/type/boolean.rb#L15-L25 ---------------------------------------- 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/ ______________________________________________ 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/