ruby picture

RCR 191: add TrueClass#not and FalseClass#not

Submitted by coreywangler (Wed Jan 14 02:01:29 UTC 2004)

Abstract

Adding TrueClass#not and FalseClass#not instance methods allows for clearer code in some cases, by allowing chaining of method calls with the ".not" method.

Problem

There is currently no ".not" method for boolean objects.

Proposal

Add TrueClass#not and FalseClass#not instance methods.

Analysis


    
  class TrueClass
    def not
      not self
    end
  end
  class FalseClass
    def not
      not self
    end
  end
  a=nil
  a.nil?.not    #==>false
  a=3
  a.nil?.not    #==>true

    

Makes it easy to express things the object-oriented way, by chaining method calls. This can make code clearer in some cases.

Implementation

Addition to the language (for speed and efficiency) the equivalent of this Ruby code...


    
  class TrueClass
    def not
      not self
    end
  end
  class FalseClass
    def not
      not self
    end
  end

    

ruby picture
Comments Current voting

Note that there are some predicates that do not return boolean values, for example, "defined?" gives string description of the expression if defined, otherwise returns nil.

Consequently, adding "not" method to TrueClass and FalseClass is not enough. Options are:

  • adding "not" method to Object class, so that every object can act as boolean value.
  • change all predicate methods to return boolean value only.
  • give up this proposal.

I don't think changing all predicates are not worthy. And I feel kind of reluctant to adding "not" to every object.

-matz.


Withdrawing RCR

Yeah, adding to Object is not good.

- corey.


Strongly opposed 0
Opposed 4
Neutral 2
In favor 2
Strongly advocate 0
ruby picture
If you have registered at RCRchive, you may now sign in below. If you have not registered, you may sign up for a username and password. Registering enables you to submit new RCRs, and vote and leave comments on existing RCRs.
Your username:
Your password:

ruby picture

Powered by .