Skip to content

Commit

Permalink
Add Numeric#pos? & Numeric#neg?
Browse files Browse the repository at this point in the history
  • Loading branch information
Bozhidar Batsov committed Aug 21, 2013
1 parent c99a733 commit c50e367
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions lib/powerpack.rb
Expand Up @@ -2,4 +2,6 @@

require 'powerpack/hash'

require 'powerpack/numeric'

require 'powerpack/string'
2 changes: 2 additions & 0 deletions lib/powerpack/numeric.rb
@@ -0,0 +1,2 @@
require_relative 'numeric/neg'
require_relative 'numeric/pos'
19 changes: 19 additions & 0 deletions lib/powerpack/numeric/neg.rb
@@ -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
19 changes: 19 additions & 0 deletions lib/powerpack/numeric/pos.rb
@@ -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
24 changes: 24 additions & 0 deletions spec/powerpack/numeric/neg_spec.rb
@@ -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
24 changes: 24 additions & 0 deletions spec/powerpack/numeric/pos_spec.rb
@@ -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.