Permalink
Browse files

Add Numeric#pos? & Numeric#neg?

  • Loading branch information...
1 parent c99a733 commit c50e367cd2cbe7bafa6db108c715f0497de44048 @bbatsov committed Aug 21, 2013
View
@@ -23,6 +23,9 @@ Or install it yourself as:
* [Hash](http://rdoc.info/github/bbatsov/powerpack/Hash)
* [#symbolize_keys](http://rdoc.info/github/bbatsov/powerpack/Hash#symbolize_keys-instance_method)
+* [Numeric](http://rdoc.info/github/bbatsov/powerpack/Numeric)
+ * [#pos?](http://rdoc.info/github/bbatsov/powerpack/Numeric#pos?-instance_method)
+ * [#neg?](http://rdoc.info/github/bbatsov/powerpack/Numeric#neg?-instance_method)
* [String](http://rdoc.info/github/bbatsov/powerpack/String)
* [#blank?](http://rdoc.info/github/bbatsov/powerpack/String#blank?-instance_method)
* [#format](http://rdoc.info/github/bbatsov/powerpack/String#format-instance_method)
View
@@ -2,4 +2,6 @@
require 'powerpack/hash'
+require 'powerpack/numeric'
+
require 'powerpack/string'
View
@@ -0,0 +1,2 @@
+require_relative 'numeric/neg'
+require_relative 'numeric/pos'
@@ -0,0 +1,19 @@
+unless Numeric.method_defined? :neg?
+ class Numeric
+ # Checks whether a number is negative.
+ #
+ # @return [Boolean] true is the number is negative, false otherwise
+ #
+ # @example
+ # 5.neg? #=> false
+ #
+ # @example
+ # -0.5.neg? #=> true
+ #
+ # @example
+ # 0.neg? #=> false
+ def neg?
+ self < 0
+ end
+ end
+end
@@ -0,0 +1,19 @@
+unless Numeric.method_defined? :pos?
+ class Numeric
+ # Checks whether a number is positive.
+ #
+ # @return [Boolean] true is the number is positive, false otherwise
+ #
+ # @example
+ # 5.pos? #=> true
+ #
+ # @example
+ # -0.5.pos? #=> false
+ #
+ # @example
+ # 0.pos? #=> false
+ def pos?
+ self > 0
+ end
+ end
+end
@@ -0,0 +1,24 @@
+require 'spec_helper'
+
+describe 'Numeric#neg?' do
+ it 'returns false for positive integer' do
+ expect(1.neg?).to be_false
+ end
+
+ it 'returns false for positive float' do
+ expect(0.1.neg?).to be_false
+ end
+
+ it 'returns true for negative integer' do
+ expect(-1.neg?).to be_true
+ end
+
+ it 'returns true for negative float' do
+ expect(-0.01.neg?).to be_true
+ end
+
+ it 'returns false for 0' do
+ expect(0.neg?).to be_false
+ expect(0.0.neg?).to be_false
+ end
+end
@@ -0,0 +1,24 @@
+require 'spec_helper'
+
+describe 'Numeric#pos?' do
+ it 'returns true for positive integer' do
+ expect(1.pos?).to be_true
+ end
+
+ it 'returns true for positive float' do
+ expect(0.1.pos?).to be_true
+ end
+
+ it 'returns false for negative integer' do
+ expect(-1.pos?).to be_false
+ end
+
+ it 'returns false for negative float' do
+ expect(-0.01.pos?).to be_false
+ end
+
+ it 'returns false for 0' do
+ expect(0.pos?).to be_false
+ expect(0.0.pos?).to be_false
+ end
+end

0 comments on commit c50e367

Please sign in to comment.