Skip to content
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.

@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.

@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.

Copy link

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.

Copy link
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.

Copy link
Member

rafaelfranca replied May 15, 2015

@simi

This comment has been minimized.

Copy link
Contributor

simi replied May 15, 2015

Ahh, thanks for clarification @rafaelfranca.

@gsamokovarov

This comment has been minimized.

Copy link
Contributor

gsamokovarov replied May 16, 2015

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

@joneslee85

This comment has been minimized.

Copy link
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.

Copy link

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.

Copy link
Member

rafaelfranca replied May 19, 2015

@cheerfulstoic ruby has nonzero? already.

@cheerfulstoic

This comment has been minimized.

Copy link

cheerfulstoic replied May 19, 2015

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

@lccezinha

This comment has been minimized.

Copy link

lccezinha replied May 21, 2015

For prime numbers, already exists prime.

Nice add!

@localhots

This comment has been minimized.

Copy link

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.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.