Skip to content

Commit

Permalink
Add Integer#positive? and Integer#negative? query methods in the vein…
Browse files Browse the repository at this point in the history
… of Fixnum#zero?
  • Loading branch information
dhh committed May 13, 2015
1 parent 07ad2a9 commit e54277a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
5 changes: 5 additions & 0 deletions activesupport/CHANGELOG.md
@@ -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.
Expand Down
1 change: 1 addition & 0 deletions activesupport/lib/active_support/core_ext/integer.rb
@@ -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'
19 changes: 19 additions & 0 deletions activesupport/lib/active_support/core_ext/integer/inquiry.rb
@@ -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.

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

Copy link
@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
12 changes: 12 additions & 0 deletions activesupport/test/core_ext/integer_ext_test.rb
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome, thanks!

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

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

@simi
Copy link
Contributor

@simi simi commented on e54277a May 15, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@rafaelfranca
Copy link
Member

@rafaelfranca rafaelfranca commented on e54277a May 15, 2015 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@simi
Copy link
Contributor

@simi simi commented on e54277a May 15, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, thanks for clarification @rafaelfranca.

@gsamokovarov
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@runlevel5
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@cheerfulstoic
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@rafaelfranca
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cheerfulstoic ruby has nonzero? already.

@cheerfulstoic
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@lcezermf
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For prime numbers, already exists prime.

Nice add!

@localhots
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Please sign in to comment.