* [ruby-core:119262] [Ruby master Feature#20756] Introduce Boolean class
@ 2024-09-19 21:50 kbrock (Keenan Brock) via ruby-core
2024-09-19 22:53 ` [ruby-core:119263] " matz (Yukihiro Matsumoto) via ruby-core
0 siblings, 1 reply; 2+ messages in thread
From: kbrock (Keenan Brock) via ruby-core @ 2024-09-19 21:50 UTC (permalink / raw)
To: ruby-core; +Cc: kbrock (Keenan Brock)
Issue #20756 has been reported by kbrock (Keenan Brock).
----------------------------------------
Feature #20756: Introduce Boolean class
https://bugs.ruby-lang.org/issues/20756
* Author: kbrock (Keenan Brock)
* Status: Open
----------------------------------------
Hello All,
Is is possible to add a parent class for `TrueClass` and `FalseClass`?
I always found it strange that there was not a concept to group `true` and `false` together.
e.g.:
```ruby
class TrueClass < Boolean ; end
class FalseClass < Boolean ; end
# replaces the hack:
module Boolean ; end
TrueClass.include(Boolean);
FalseClass.include(Boolean);
```
In our code, we often want to validate that a value is of a certain type.
```ruby
case(value)
when String ...
when Integer ...
when true, false ... # Boolean
# alt: when TrueClass, FalseClass
end
# or
def valid_args?
value.kind_of?(String)
end
def valid_args?
value.kind_of?(Integer)
end
def valid_args?
[true, false].include?(value)
[TrueClass, FalseClass].detect { |klass| value.kind_of?(klass) }
end
```
Does it make sense to others to have a way to group `true` and `false` together?
Thank you for your thoughts,
Keenan
--
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] 2+ messages in thread
* [ruby-core:119263] [Ruby master Feature#20756] Introduce Boolean class
2024-09-19 21:50 [ruby-core:119262] [Ruby master Feature#20756] Introduce Boolean class kbrock (Keenan Brock) via ruby-core
@ 2024-09-19 22:53 ` matz (Yukihiro Matsumoto) via ruby-core
0 siblings, 0 replies; 2+ messages in thread
From: matz (Yukihiro Matsumoto) via ruby-core @ 2024-09-19 22:53 UTC (permalink / raw)
To: ruby-core; +Cc: matz (Yukihiro Matsumoto)
Issue #20756 has been updated by matz (Yukihiro Matsumoto).
Status changed from Open to Rejected
#12515 is an enough reason to reject this idea.
> Rejected for several reasons:
>
> * many gems and libraries had already introduced Boolean class. I don't want to break them.
> * true and false are the only representative of true-false values. In Ruby. nil and false are falsy values, and everything else is a true value. There's no meaning for having a superclass of TrueClass and FalseClass as Boolean.
Even if we introduced `Boolean` class, it'd only used to check type/class using `kind_of?` (which is against duck-typing principle). Since we have no common behavior/implementation of boolean objects.
Matz.
----------------------------------------
Feature #20756: Introduce Boolean class
https://bugs.ruby-lang.org/issues/20756#change-109857
* Author: kbrock (Keenan Brock)
* Status: Rejected
----------------------------------------
Hello All,
Is is possible to add a parent class for `TrueClass` and `FalseClass`?
I always found it strange that there was not a concept to group `true` and `false` together.
e.g.:
```ruby
class TrueClass < Boolean ; end
class FalseClass < Boolean ; end
# replaces the hack:
module Boolean ; end
TrueClass.include(Boolean);
FalseClass.include(Boolean);
```
In our code, we often want to validate that a value is of a certain type.
```ruby
case(value)
when String ...
when Integer ...
when true, false ... # Boolean
# alt: when TrueClass, FalseClass
end
# or
def valid_args?
value.kind_of?(String)
end
def valid_args?
value.kind_of?(Integer)
end
def valid_args?
[true, false].include?(value)
[TrueClass, FalseClass].detect { |klass| value.kind_of?(klass) }
end
```
Does it make sense to others to have a way to group `true` and `false` together?
Thank you for your thoughts,
Keenan
--
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] 2+ messages in thread
end of thread, other threads:[~2024-09-19 22:54 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-09-19 21:50 [ruby-core:119262] [Ruby master Feature#20756] Introduce Boolean class kbrock (Keenan Brock) via ruby-core
2024-09-19 22:53 ` [ruby-core:119263] " 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).