From: "Dan0042 (Daniel DeLorme) via ruby-core" Date: 2024-11-11T21:27:46+00:00 Subject: [ruby-core:119878] [Ruby master Feature#20882] Provide Boolean(...) 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/ ______________________________________________ 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/