Project

General

Profile

Actions

Feature #20756

closed

Introduce Boolean class

Added by kbrock (Keenan Brock) about 2 months ago. Updated about 2 months ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-core:119262]

Description

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.:

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.

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


Related issues 3 (0 open3 closed)

Related to Ruby master - Feature #14224: Boolean classRejectedActions
Related to Ruby master - Bug #12827: Add Boolean data type.RejectedActions
Related to Ruby master - Feature #12515: Create "Boolean" superclass of TrueClass / FalseClassRejectedActions
Actions #1

Updated by byroot (Jean Boussier) about 2 months ago

Actions #2

Updated by byroot (Jean Boussier) about 2 months ago

  • Related to Bug #12827: Add Boolean data type. added
Actions #3

Updated by byroot (Jean Boussier) about 2 months ago

  • Related to Feature #12515: Create "Boolean" superclass of TrueClass / FalseClass added

Updated by matz (Yukihiro Matsumoto) about 2 months ago

  • 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.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0