Issue #9123 has been updated by mame (Yusuke Endoh).


I don't think it is possible to change the spec.
Many programs in the wild actually use the behavior.

https://github.com/search?q=nonzero%3F+sort+extension%3Arb&type=Code



By the way, I investigated the early history of nonzero?.


In the beginning, Nagai proposed a "<>" operator that returns -1, nil, 1
in [ruby-list:7286] (1 Apr 1998).  The motivation example was:

  def struct_cmp (a, b)
     (a.mem_a <> a.mem_a) || (a.mem_b <=> a.mem_b)
  end

Obviously he had in mind Perl's common idiom for lexicographic sort:

  sort { a[0] <=> b[0] || a[1] <=> b[1] }

because it is 1998 :-)


Matz said he was negative to this proposal in [ruby-list:7286] because
it would make the language complex to add a new operator, and because
he thought that it would be better to change the boolean semantics,
that is, to handle 0 as false (!).


Nagai, in [ruby-dev:2026], disagreed with the semantic change because of
a philosophical reason (it looked weird to him to handle only one instance
of Integer as false), and reclaimed adding a new operator.


In [ruby-dev:2031], Funaba disagreed with a new operator because the
lexicographic sort is not a common operation, he thought.
He also disagreed with the semantic change because of a compatibility
issue (in spite of 1998).
And then, nonzero? appeared in the counter-proposal he made:

  (a <=> b).nonzero? || (a2 <=> b2)


Matz liked and implemented the proposal in a day [ruby-dev:2046].


Because nonzero? was accepted, a "<>" operator became less significant.
Nagai, in [ruby-dev:2050], dismissed his proposal.

In the mail, he pointed the inconsistency that is now discussed in this
ticket.  He asked why matz implemented not only nonzero? but also zero?.
Matz's answer was:

- symmetry of zero? and nonzero?
- lisp/scheme provides zero?

(According to this history, it makes no sense to fit nonzero? with zero?.)


Incidentally, the discussion continued, and Keiju proposed Array#<=> in
[ruby-dev:2101], that is,

  [a1, a2] <=> [b1, b2]

After the further discussion, this was accepted too.

Funaba, who proposed nonzero? himself, preferred Array#<=> to nonzero?,
but also pointed out that Array#<=> is not a perfect substitute to
nonzero? because Array#<=> does not shortcut.


After that, nonzero? exists until now.

-- 
Yusuke Endoh <mame / tsg.ne.jp>
----------------------------------------
Feature #9123: Make Numeric#nonzero? behavior consistent with Numeric#zero?
https://bugs.ruby-lang.org/issues/9123#change-43083

Author: sferik (Erik Michaels-Ober)
Status: Open
Priority: Normal
Assignee: 
Category: 
Target version: 


Numeric#zero? returns true or false, while Numeric#nonzero? returns self or nil.

I've written a patch that fixes this inconsistency and adds a Numeric#nonzero (non-predicate) method that returns self or nil for chaining comparisons. I'd like for this to be included in Ruby 2.1.0.

https://github.com/ruby/ruby/pull/452.patch


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