Permalink
Browse files

Add Integer#positive? and Integer#negative? query methods in the vein…

… of Fixnum#zero?
  • Loading branch information...
dhh committed May 13, 2015
1 parent 07ad2a9 commit e54277a45da3c86fecdfa930663d7692fd083daa
@@ -1,3 +1,8 @@
* Add Integer#positive? and Integer#negative? query methods in the vein of Fixnum#zero?
This makes it nicer to do things like bunch_of_numbers.select(&:positive?).
*DHH*
* Encoding ActiveSupport::TimeWithZone to YAML now preserves the timezone information.
Fixes #9183.
@@ -1,3 +1,4 @@
require 'active_support/core_ext/integer/multiple'
require 'active_support/core_ext/integer/inflections'
require 'active_support/core_ext/integer/inquiry'
require 'active_support/core_ext/integer/time'
@@ -0,0 +1,19 @@
class Integer
# Returns true if the number is positive.
#
# 1.positive? # => true
# 0.positive? # => false
# -1.positive? # => false
def positive?
self > 0
end
# Returns true if the number is positive.

This comment has been minimized.

Show comment
Hide comment
@leandrocp

leandrocp May 16, 2015

Could change this doc to reflect method name ? /cc @dhh

#   -1.negative? # => true
#   0.negative?  # => false
#   1.negative?  # => false
@leandrocp

leandrocp May 16, 2015

Could change this doc to reflect method name ? /cc @dhh

#   -1.negative? # => true
#   0.negative?  # => false
#   1.negative?  # => false

This comment has been minimized.

Show comment
Hide comment
@prathamesh-sonpatki

prathamesh-sonpatki May 16, 2015

Member

It's fixed on master.

@prathamesh-sonpatki

prathamesh-sonpatki May 16, 2015

Member

It's fixed on master.

#
# -1.positive? # => true
# 0.positive? # => false
# 1.positive? # => false
def negative?
self < 0
end
end
@@ -27,4 +27,16 @@ def test_ordinal
assert_equal 'st', 1.ordinal
assert_equal 'th', 8.ordinal
end
def test_positive
assert 1.positive?
assert_not 0.positive?
assert_not -1.positive?
end
def test_negative
assert -1.negative?
assert_not 0.negative?
assert_not 1.negative?
end
end

11 comments on commit e54277a

@cheerfulstoic

This comment has been minimized.

Show comment
Hide comment
@cheerfulstoic

cheerfulstoic May 15, 2015

This is awesome, thanks!

By the way, could we get a #prime? too?

(but seriously, '#non_zero?` would be awesome too!)

cheerfulstoic replied May 15, 2015

This is awesome, thanks!

By the way, could we get a #prime? too?

(but seriously, '#non_zero?` would be awesome too!)

@simi

This comment has been minimized.

Show comment
Hide comment
@simi

simi May 15, 2015

Contributor

I thought AS additions are frozen for code not used internally in Rails.

Contributor

simi replied May 15, 2015

I thought AS additions are frozen for code not used internally in Rails.

@rafaelfranca

This comment has been minimized.

Show comment
Hide comment
@rafaelfranca

rafaelfranca May 15, 2015

Member
Member

rafaelfranca replied May 15, 2015

@simi

This comment has been minimized.

Show comment
Hide comment
@simi

simi May 15, 2015

Contributor

Ahh, thanks for clarification @rafaelfranca.

Contributor

simi replied May 15, 2015

Ahh, thanks for clarification @rafaelfranca.

@gsamokovarov

This comment has been minimized.

Show comment
Hide comment
@gsamokovarov

gsamokovarov May 16, 2015

Contributor

@cheerfulstoic #prime? comes from Ruby standard library already. Try requiring prime.

Contributor

gsamokovarov replied May 16, 2015

@cheerfulstoic #prime? comes from Ruby standard library already. Try requiring prime.

@joneslee85

This comment has been minimized.

Show comment
Hide comment
@joneslee85

joneslee85 May 17, 2015

Contributor

Such great addition. Why stopping here? I think Ruby core lib needs this love too.

Contributor

joneslee85 replied May 17, 2015

Such great addition. Why stopping here? I think Ruby core lib needs this love too.

@cheerfulstoic

This comment has been minimized.

Show comment
Hide comment
@cheerfulstoic

cheerfulstoic May 17, 2015

@gsamokovarov Thanks, I had forgotten about that! I was joking about #prime?, but was (more) serious about #non_zero?

cheerfulstoic replied May 17, 2015

@gsamokovarov Thanks, I had forgotten about that! I was joking about #prime?, but was (more) serious about #non_zero?

@rafaelfranca

This comment has been minimized.

Show comment
Hide comment
@rafaelfranca

rafaelfranca May 19, 2015

Member

@cheerfulstoic ruby has nonzero? already.

Member

rafaelfranca replied May 19, 2015

@cheerfulstoic ruby has nonzero? already.

@cheerfulstoic

This comment has been minimized.

Show comment
Hide comment
@cheerfulstoic

cheerfulstoic May 19, 2015

Right, so it does! I'll shut up now ;)

cheerfulstoic replied May 19, 2015

Right, so it does! I'll shut up now ;)

@lccezinha

This comment has been minimized.

Show comment
Hide comment
@lccezinha

lccezinha May 21, 2015

For prime numbers, already exists prime.

Nice add!

lccezinha replied May 21, 2015

For prime numbers, already exists prime.

Nice add!

@localhots

This comment has been minimized.

Show comment
Hide comment
@localhots

localhots May 21, 2015

I've made this gem as a joke four years ago, and now Rails got serious about it. Marvelous!

localhots replied May 21, 2015

I've made this gem as a joke four years ago, and now Rails got serious about it. Marvelous!

Please sign in to comment.