Project

General

Profile

Actions

Feature #11151

closed

Numeric#positive? and Numeric#negative?

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

Status:
Closed
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

Updated by usa (Usaku NAKAMURA) almost 9 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.

Updated by rafaelfranca (Rafael França) almost 9 years ago

Right. Thank you for the explanation.

So maybe just to Fixnum and Float?

Updated by phluid61 (Matthew Kerwin) almost 9 years ago

On 14/05/2015, 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/

Updated by rafaelfranca (Rafael França) almost 9 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

Updated by matz (Yukihiro Matsumoto) almost 9 years ago

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

Matz.

Actions #6

Updated by nobu (Nobuyoshi Nakada) almost 9 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?.
    [ruby-core:69173] [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
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0