ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:119852] [Ruby master Feature#20882] Provide Boolean(...)
@ 2024-11-08 22:28 getajobmike (Mike Perham) via ruby-core
  2024-11-09  0:19 ` [ruby-core:119854] " ioquatix (Samuel Williams) via ruby-core
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: getajobmike (Mike Perham) via ruby-core @ 2024-11-08 22:28 UTC (permalink / raw)
  To: ruby-core; +Cc: getajobmike (Mike Perham)

Issue #20882 has been reported by getajobmike (Mike Perham).

----------------------------------------
Feature #20882: Provide Boolean(...)
https://bugs.ruby-lang.org/issues/20882

* 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/

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [ruby-core:119854] [Ruby master Feature#20882] Provide Boolean(...)
  2024-11-08 22:28 [ruby-core:119852] [Ruby master Feature#20882] Provide Boolean(...) getajobmike (Mike Perham) via ruby-core
@ 2024-11-09  0:19 ` ioquatix (Samuel Williams) via ruby-core
  2024-11-09  1:01 ` [ruby-core:119856] " bkuhlmann (Brooke Kuhlmann) via ruby-core
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: ioquatix (Samuel Williams) via ruby-core @ 2024-11-09  0:19 UTC (permalink / raw)
  To: ruby-core; +Cc: ioquatix (Samuel Williams)

Issue #20882 has been updated by ioquatix (Samuel Williams).


I am the current maintainer of the `boolean` gem. You can check the implementation here: https://github.com/ioquatix/boolean

I think `Boolean` can be adopted as a core Ruby class, but I think it has been discussed before and rejected because it only has two values. However, for type annotation and type coercion, it can be useful.

----------------------------------------
Feature #20882: Provide Boolean(...)
https://bugs.ruby-lang.org/issues/20882#change-110544

* 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/

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [ruby-core:119856] [Ruby master Feature#20882] Provide Boolean(...)
  2024-11-08 22:28 [ruby-core:119852] [Ruby master Feature#20882] Provide Boolean(...) getajobmike (Mike Perham) via ruby-core
  2024-11-09  0:19 ` [ruby-core:119854] " ioquatix (Samuel Williams) via ruby-core
@ 2024-11-09  1:01 ` bkuhlmann (Brooke Kuhlmann) via ruby-core
  2024-11-09  5:18 ` [ruby-core:119860] " HarlemSquirrel (Kevin McCormack) via ruby-core
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: bkuhlmann (Brooke Kuhlmann) via ruby-core @ 2024-11-09  1:01 UTC (permalink / raw)
  To: ruby-core; +Cc: bkuhlmann (Brooke Kuhlmann)

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/

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [ruby-core:119860] [Ruby master Feature#20882] Provide Boolean(...)
  2024-11-08 22:28 [ruby-core:119852] [Ruby master Feature#20882] Provide Boolean(...) getajobmike (Mike Perham) via ruby-core
  2024-11-09  0:19 ` [ruby-core:119854] " ioquatix (Samuel Williams) via ruby-core
  2024-11-09  1:01 ` [ruby-core:119856] " bkuhlmann (Brooke Kuhlmann) via ruby-core
@ 2024-11-09  5:18 ` HarlemSquirrel (Kevin McCormack) via ruby-core
  2024-11-09  9:48 ` [ruby-core:119862] " murb (Maarten Brouwers) via ruby-core
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: HarlemSquirrel (Kevin McCormack) via ruby-core @ 2024-11-09  5:18 UTC (permalink / raw)
  To: ruby-core; +Cc: HarlemSquirrel (Kevin McCormack)

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/

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [ruby-core:119862] [Ruby master Feature#20882] Provide Boolean(...)
  2024-11-08 22:28 [ruby-core:119852] [Ruby master Feature#20882] Provide Boolean(...) getajobmike (Mike Perham) via ruby-core
                   ` (2 preceding siblings ...)
  2024-11-09  5:18 ` [ruby-core:119860] " HarlemSquirrel (Kevin McCormack) via ruby-core
@ 2024-11-09  9:48 ` murb (Maarten Brouwers) via ruby-core
  2024-11-09 12:34 ` [ruby-core:119865] " Earlopain (A S) via ruby-core
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: murb (Maarten Brouwers) via ruby-core @ 2024-11-09  9:48 UTC (permalink / raw)
  To: ruby-core; +Cc: murb (Maarten Brouwers)

Issue #20882 has been updated by murb (Maarten Brouwers).


+1 for the request, I don't like to resorting to `ActiveRecord::Type::Boolean.new.cast(value)` to cast booleans. I would prefer a more explicit `.parse` which DateTime has though:

```
DateTime.parse("2021-04-13T14:20")
```


```
Boolean.parse("0") # false
Boolean.parse(1) # true
Boolean.parse("t") # true
```

----------------------------------------
Feature #20882: Provide Boolean(...)
https://bugs.ruby-lang.org/issues/20882#change-110552

* 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/

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [ruby-core:119865] [Ruby master Feature#20882] Provide Boolean(...)
  2024-11-08 22:28 [ruby-core:119852] [Ruby master Feature#20882] Provide Boolean(...) getajobmike (Mike Perham) via ruby-core
                   ` (3 preceding siblings ...)
  2024-11-09  9:48 ` [ruby-core:119862] " murb (Maarten Brouwers) via ruby-core
@ 2024-11-09 12:34 ` Earlopain (A S) via ruby-core
  2024-11-10 19:13 ` [ruby-core:119874] " bunnrf (RF Bunn) via ruby-core
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Earlopain (A S) via ruby-core @ 2024-11-09 12:34 UTC (permalink / raw)
  To: ruby-core; +Cc: Earlopain (A S)

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/

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [ruby-core:119874] [Ruby master Feature#20882] Provide Boolean(...)
  2024-11-08 22:28 [ruby-core:119852] [Ruby master Feature#20882] Provide Boolean(...) getajobmike (Mike Perham) via ruby-core
                   ` (4 preceding siblings ...)
  2024-11-09 12:34 ` [ruby-core:119865] " Earlopain (A S) via ruby-core
@ 2024-11-10 19:13 ` bunnrf (RF Bunn) via ruby-core
  2024-11-10 20:24 ` [ruby-core:119875] " austin (Austin Ziegler) via ruby-core
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: bunnrf (RF Bunn) via ruby-core @ 2024-11-10 19:13 UTC (permalink / raw)
  To: ruby-core; +Cc: bunnrf (RF Bunn)

Issue #20882 has been updated by bunnrf (RF Bunn).


This was rejected 2 months ago
https://bugs.ruby-lang.org/issues/20756

----------------------------------------
Feature #20882: Provide Boolean(...)
https://bugs.ruby-lang.org/issues/20882#change-110565

* 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/

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [ruby-core:119875] [Ruby master Feature#20882] Provide Boolean(...)
  2024-11-08 22:28 [ruby-core:119852] [Ruby master Feature#20882] Provide Boolean(...) getajobmike (Mike Perham) via ruby-core
                   ` (5 preceding siblings ...)
  2024-11-10 19:13 ` [ruby-core:119874] " bunnrf (RF Bunn) via ruby-core
@ 2024-11-10 20:24 ` austin (Austin Ziegler) via ruby-core
  2024-11-11 21:27 ` [ruby-core:119878] " Dan0042 (Daniel DeLorme) via ruby-core
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: austin (Austin Ziegler) via ruby-core @ 2024-11-10 20:24 UTC (permalink / raw)
  To: ruby-core; +Cc: austin (Austin Ziegler)

Issue #20882 has been updated by austin (Austin Ziegler).


bunnrf (RF Bunn) wrote in #note-6:
> This was rejected 2 months ago
> https://bugs.ruby-lang.org/issues/20756
> 
> Previously
> https://bugs.ruby-lang.org/issues/13260

This is not about a Boolean type, but a Boolean coercion function, `Kernel#Boolean` or a casting function, `Object#to_bool`.

I support the concept of a coercion function, but think that if string coercion is down, only case-insensitive true/false values should be supported and possibly 0/1 values. Under no circumstances should we make the NOrway mistake that YAML before 1.2 did. (I believe YAML 1.2 supports True, False, TRUE, FALSE, true, and false only.)

This would be a good set, IMO. I don’t believe that the Rails / ActiveSupport approach is correct (although supporting symbol versions of the strings would be ok).

----------------------------------------
Feature #20882: Provide Boolean(...)
https://bugs.ruby-lang.org/issues/20882#change-110566

* 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/

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [ruby-core:119878] [Ruby master Feature#20882] Provide Boolean(...)
  2024-11-08 22:28 [ruby-core:119852] [Ruby master Feature#20882] Provide Boolean(...) getajobmike (Mike Perham) via ruby-core
                   ` (6 preceding siblings ...)
  2024-11-10 20:24 ` [ruby-core:119875] " austin (Austin Ziegler) via ruby-core
@ 2024-11-11 21:27 ` Dan0042 (Daniel DeLorme) via ruby-core
  2024-11-12  0:04 ` [ruby-core:119879] " austin (Austin Ziegler) via ruby-core
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Dan0042 (Daniel DeLorme) via ruby-core @ 2024-11-11 21:27 UTC (permalink / raw)
  To: ruby-core; +Cc: Dan0042 (Daniel DeLorme)

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/

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [ruby-core:119879] [Ruby master Feature#20882] Provide Boolean(...)
  2024-11-08 22:28 [ruby-core:119852] [Ruby master Feature#20882] Provide Boolean(...) getajobmike (Mike Perham) via ruby-core
                   ` (7 preceding siblings ...)
  2024-11-11 21:27 ` [ruby-core:119878] " Dan0042 (Daniel DeLorme) via ruby-core
@ 2024-11-12  0:04 ` austin (Austin Ziegler) via ruby-core
  2024-11-12  1:13 ` [ruby-core:119880] " duerst via ruby-core
  2024-12-14  5:29 ` [ruby-core:120237] " matz (Yukihiro Matsumoto) via ruby-core
  10 siblings, 0 replies; 12+ messages in thread
From: austin (Austin Ziegler) via ruby-core @ 2024-11-12  0:04 UTC (permalink / raw)
  To: ruby-core; +Cc: austin (Austin Ziegler)

Issue #20882 has been updated by austin (Austin Ziegler).


Dan0042 (Daniel DeLorme) wrote in #note-8:
> 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.

GitHub Actions takes what I think is the right choice for this:

```typescript
/**
 * Gets the input value of the boolean type in the YAML 1.2 "core schema" specification.
 * Support boolean input list: `true | True | TRUE | false | False | FALSE` .
 * The return value is also in boolean type.
 * ref: https://yaml.org/spec/1.2/spec.html#id2804923
 *
 * @param     name     name of the input to get
 * @param     options  optional. See InputOptions.
 * @returns   boolean
 */
export function getBooleanInput(name: string, options?: InputOptions): boolean {
  const trueValue = ['true', 'True', 'TRUE']
  const falseValue = ['false', 'False', 'FALSE']
  const val = getInput(name, options)
  if (trueValue.includes(val)) return true
  if (falseValue.includes(val)) return false
  throw new TypeError(
    `Input does not meet YAML 1.2 "Core Schema" specification: ${name}\n` +
      `Support boolean input list: \`true | True | TRUE | false | False | FALSE\``
  )
}
```

In the main cases where this is needed, one is pulling from an environment variable (which is always a string) or similar input. `Kernel#Integer` throws an exception if the value does not match the expectation of an integer (it is fairly *liberal* with what makes an integer, accepting decimal, binary, hex, and octal inputs). There is no reason that an equivalent `Kernel#Boolean` could not do the same and do so with exactly the six values listed above, *possibly* twelve if we wanted to accept symbol versions as well. I would argue that any other interpretation of a "boolean string" should be handled explicitly by calling `#==` or `#===` (via case) or with pattern matching because otherwise you are opening yourself to invalid or unexpected inputs, which are ultimately security risks.

----------------------------------------
Feature #20882: Provide Boolean(...)
https://bugs.ruby-lang.org/issues/20882#change-110571

* 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/

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [ruby-core:119880] [Ruby master Feature#20882] Provide Boolean(...)
  2024-11-08 22:28 [ruby-core:119852] [Ruby master Feature#20882] Provide Boolean(...) getajobmike (Mike Perham) via ruby-core
                   ` (8 preceding siblings ...)
  2024-11-12  0:04 ` [ruby-core:119879] " austin (Austin Ziegler) via ruby-core
@ 2024-11-12  1:13 ` duerst via ruby-core
  2024-12-14  5:29 ` [ruby-core:120237] " matz (Yukihiro Matsumoto) via ruby-core
  10 siblings, 0 replies; 12+ messages in thread
From: duerst via ruby-core @ 2024-11-12  1:13 UTC (permalink / raw)
  To: ruby-core; +Cc: duerst

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/

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [ruby-core:120237] [Ruby master Feature#20882] Provide Boolean(...)
  2024-11-08 22:28 [ruby-core:119852] [Ruby master Feature#20882] Provide Boolean(...) getajobmike (Mike Perham) via ruby-core
                   ` (9 preceding siblings ...)
  2024-11-12  1:13 ` [ruby-core:119880] " duerst via ruby-core
@ 2024-12-14  5:29 ` matz (Yukihiro Matsumoto) via ruby-core
  10 siblings, 0 replies; 12+ messages in thread
From: matz (Yukihiro Matsumoto) via ruby-core @ 2024-12-14  5:29 UTC (permalink / raw)
  To: ruby-core; +Cc: matz (Yukihiro Matsumoto)

Issue #20882 has been updated by matz (Yukihiro Matsumoto).

Status changed from Open to Rejected

I don't think it works well in the Ruby ecosystem. Rejected (as previous proposals).

Matz.


----------------------------------------
Feature #20882: Provide Boolean(...)
https://bugs.ruby-lang.org/issues/20882#change-111005

* Author: getajobmike (Mike Perham)
* Status: Rejected
----------------------------------------
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/

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2024-12-14  5:29 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-11-08 22:28 [ruby-core:119852] [Ruby master Feature#20882] Provide Boolean(...) getajobmike (Mike Perham) via ruby-core
2024-11-09  0:19 ` [ruby-core:119854] " ioquatix (Samuel Williams) via ruby-core
2024-11-09  1:01 ` [ruby-core:119856] " bkuhlmann (Brooke Kuhlmann) via ruby-core
2024-11-09  5:18 ` [ruby-core:119860] " HarlemSquirrel (Kevin McCormack) via ruby-core
2024-11-09  9:48 ` [ruby-core:119862] " murb (Maarten Brouwers) via ruby-core
2024-11-09 12:34 ` [ruby-core:119865] " Earlopain (A S) via ruby-core
2024-11-10 19:13 ` [ruby-core:119874] " bunnrf (RF Bunn) via ruby-core
2024-11-10 20:24 ` [ruby-core:119875] " austin (Austin Ziegler) via ruby-core
2024-11-11 21:27 ` [ruby-core:119878] " Dan0042 (Daniel DeLorme) via ruby-core
2024-11-12  0:04 ` [ruby-core:119879] " austin (Austin Ziegler) via ruby-core
2024-11-12  1:13 ` [ruby-core:119880] " duerst via ruby-core
2024-12-14  5:29 ` [ruby-core:120237] " matz (Yukihiro Matsumoto) via ruby-core

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).