From: "HarlemSquirrel (Kevin McCormack) via ruby-core" Date: 2024-11-09T05:18:52+00:00 Subject: [ruby-core:119860] [Ruby master Feature#20882] Provide Boolean(...) 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/ ______________________________________________ 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/