[ruby-core:119856] [Ruby master Feature#20882] Provide Boolean(...)
From:
"bkuhlmann (Brooke Kuhlmann) via ruby-core" <ruby-core@...>
Date:
2024-11-09 01:01:21 UTC
List:
ruby-core #119856
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/
______________________________________________
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/