Project

General

Profile

Feature #11151

Numeric#positive? and Numeric#negative?

Added by rafaelfranca (Rafael França) almost 2 years ago. Updated almost 2 years ago.

Status:
Closed
Priority:
Normal
Target version:
-
[ruby-core:69173]

Description

We just added Interger#positive? and Interger#negative? to Active Support.

I was wondering if we could get that implemented in Ruby itself and searched if it was already requested before to Ruby code.

I found that it was requested in #5513, but rejected. Since they were requested with more methods, and I don't know Japanese enough to see if there are technical reasons, I thought to request just these two methods again.

The implementation would be something like:

def positive?
  self > 0
end

def negative?
  self < 0
end

And one of its use case is filtering, like:

bunch_of_numbers.select(&:positive?)

If this feature is accepted I can work in a patch.

Related #5513

Associated revisions

Revision 50522
Added by nobu (Nobuyoshi Nakada) almost 2 years ago

numeric.c: Numeric#positive? and Numeric#negative?

  • numeric.c (num_positive_p, num_negative_p): add methods Numeric#positive? and Numeric#negative?. [Feature #11151]
  • numeric.c (flo_positive_p, flo_negative_p): specialiazed functions for Float.
  • complex.c (Init_Complex): Complex do not have positive? and negative? methods

Revision 50522
Added by nobu (Nobuyoshi Nakada) almost 2 years ago

numeric.c: Numeric#positive? and Numeric#negative?

  • numeric.c (num_positive_p, num_negative_p): add methods Numeric#positive? and Numeric#negative?. [Feature #11151]
  • numeric.c (flo_positive_p, flo_negative_p): specialiazed functions for Float.
  • complex.c (Init_Complex): Complex do not have positive? and negative? methods

Revision 50538
Added by nobu (Nobuyoshi Nakada) almost 2 years ago

numeric.c: Bignum 0 is not positive

Revision 50538
Added by nobu (Nobuyoshi Nakada) almost 2 years ago

numeric.c: Bignum 0 is not positive

Revision 50539
Added by nobu (Nobuyoshi Nakada) almost 2 years ago

numeric.c: return true

  • numeric.c (num_positive_p): return true instead of Fixnum 0. [Feature #11151]

Revision 50539
Added by nobu (Nobuyoshi Nakada) almost 2 years ago

numeric.c: return true

  • numeric.c (num_positive_p): return true instead of Fixnum 0. [Feature #11151]

History

#1 [ruby-core:69174] Updated by usa (Usaku NAKAMURA) almost 2 years ago

In #5113, matz said

  • We can use > 0 and < 0 for the purpose.
  • Complex is Numeric, but we cannot define positive? and negative? for it.

The latter is just appropriate comment, I think.

#2 [ruby-core:69177] Updated by rafaelfranca (Rafael França) almost 2 years ago

Right. Thank you for the explanation.

So maybe just to Fixnum and Float?

#3 [ruby-core:69178] Updated by phluid61 (Matthew Kerwin) almost 2 years ago

On 14/05/2015, rafaelmfranca@gmail.com rafaelmfranca@gmail.com wrote:

Issue #11151 has been updated by Rafael França.

Right. Thank you for the explanation.

So maybe just to Fixnum and Float?

You probably mean Integer and Float. And possibly also Rational.

That, or add it to Numeric and have it raise in Complex.

--
Matthew Kerwin
http://matthew.kerwin.net.au/

#4 [ruby-core:69181] Updated by rafaelfranca (Rafael França) almost 2 years ago

You probably mean Integer and Float. And possibly also Rational.

Yeah.

For what I could see, probably we'll just need to publish two functions that we are already using internally. https://github.com/ruby/ruby/blob/d77f4934f8ede19d8a97d4bc748c92e9b8883133/numeric.c#L166 and https://github.com/ruby/ruby/blob/d77f4934f8ede19d8a97d4bc748c92e9b8883133/numeric.c#L182

#5 [ruby-core:69189] Updated by matz (Yukihiro Matsumoto) almost 2 years ago

Realistic use-case is written. Accepted.
But it should recognize complex numbers (should raise exception).

Matz.

#6 Updated by nobu (Nobuyoshi Nakada) almost 2 years ago

  • Status changed from Open to Closed

Applied in changeset r50522.


numeric.c: Numeric#positive? and Numeric#negative?

  • numeric.c (num_positive_p, num_negative_p): add methods Numeric#positive? and Numeric#negative?. [Feature #11151]
  • numeric.c (flo_positive_p, flo_negative_p): specialiazed functions for Float.
  • complex.c (Init_Complex): Complex do not have positive? and negative? methods

Also available in: Atom PDF