Feature #11151

Numeric#positive? and Numeric#negative?

Rafael França6日前に追加. 3日前に更新.

[ruby-core:69173]
ステータス:Closed
優先度:Normal
担当者:Yukihiro Matsumoto

説明

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

関係しているリビジョン

リビジョン 50522
Nobuyoshi Nakada3日前に追加

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

リビジョン 50538
Nobuyoshi Nakada約16時間前に追加

numeric.c: Bignum 0 is not positive

リビジョン 50539
Nobuyoshi Nakada約16時間前に追加

numeric.c: return true

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

履歴

#1 Usaku NAKAMURA6日前に更新

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 Rafael França6日前に更新

Right. Thank you for the explanation.

So maybe just to Fixnum and Float?

#3 Matthew Kerwin6日前に更新

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 Rafael França6日前に更新

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 Yukihiro Matsumoto6日前に更新

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

Matz.

#6 Nobuyoshi Nakada3日前に更新

  • ステータスOpen から 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

他の形式にエクスポート: Atom PDF