Issue #12515 has been updated by Samuel Williams.


> But that's how things already are, and the market deals with it just fine. 

Well, I know you are being metaphorical ("market deals with it"). But this is not really a case of a market, and I prefer DRY for many reasons. There should be one correct implementation and it makes sense for it to be within Ruby.

To be explicit about it: If I include two gems, and they both define "class Boolean", and they collide for some reason, it's a problem for me, and it might not even be an obvious problem.

> What "something" would you do that is identical for both True and False?

Sometimes you need to handle multiple data types this way because the functionality is on core classes that you don't want to extend. It's similar to how type classes work in Haskell.

So, as a basic example, sometimes you want to be declarative about, say, an API:

~~~
format_response(name: String, age: Integer, happy: Boolean)
~~~

Another example of this would be libraries like optparse, slop, sequel, etc, where they'd like to declaratively specify that something is a Boolean, e.g.

~~~
option '--foo', type: Boolean

# or

create_table :foo do |t|
    t.happy type: Boolean
end
~~~

Another use case would be where you want to do validation, conversion or deal with a type as specified by the user, e.g.

~~~
case @type
when Integer
    Integer(result)
when Float
    Float(result)
when Boolean
    Boolean(result)
end
~~~

Unfortunately in Ruby, the type constructors (`Kernel.Integer`) are free functions, not part of the class, e.g you can't write `Integer.parse(input)`. You need to write (verbosely on purpose) `Kernel.Integer(input)`. It's not very OO.

Another case would be handling the above, e.g. `Boolean.parse(input)` or `Kernel.Boolean(input)`. It would make sense if they could turn the string representations into boolean values and throw an `ArgumentError` if not convertable.






----------------------------------------
Feature #12515: Create "Boolean" superclass of TrueClass / FalseClass
https://bugs.ruby-lang.org/issues/12515#change-61127

* Author: Loren Segal
* Status: Rejected
* Priority: Normal
* Assignee: 
----------------------------------------
Since Ruby 2.4 is unifying Bignum/Fixnum into Integer (https://bugs.ruby-lang.org/issues/12005), it seems reasonable to do something similar for TrueClass / FalseClass, and create a proper Boolean hierarchy. The implementation would be fairly straightforward and should be back compat unless someone already has a "Boolean" class at toplevel out there.

Given the compatibility implications of Integer, this Boolean proposal is even less intrusive.

Sample implementation:

~~~
class Boolean < BasicObject; end
class TrueClass < Boolean; end
class FalseClass < Boolean; end
~~~




-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>