Skip to content

Commit

Permalink
Modified #truthy? logic to look for other representations of truth va…
Browse files Browse the repository at this point in the history
…lues
  • Loading branch information
jonkarna committed Apr 30, 2012
1 parent 69d2196 commit eab17c0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
truthiness (0.0.1)
truthiness (0.1.1)

GEM
remote: http://rubygems.org/
Expand Down
13 changes: 10 additions & 3 deletions lib/truthiness.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
module Truthiness

def truthy?
!!self
self.to_s.match(/true|yes|1/i) != nil
end

def not_truthy?
!self
!truthy?
end

def falsey?
self.to_s.match(/false|no|0/i) != nil
end

def not_falsey?
!falsey?
end
alias_method :falsey?, :not_truthy?

def true?
self == true
Expand Down
2 changes: 1 addition & 1 deletion lib/truthiness/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Truthiness
VERSION = "0.0.1"
VERSION = "0.1.1"
end
24 changes: 21 additions & 3 deletions test/unit/truthiness_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,27 @@ class TruthinessTest < Test::Unit::TestCase
subject { Object.new }

should "respond to truthy methods" do
[:truthy?, :not_truthy?, :falsey?,
:true?, :not_true?, :false?, :not_false?].each do |m|
assert subject.respond_to?(m)
%w[truthy? not_truthy? falsey? not_falsey?
true? not_true? false? not_false?].each do |m|
assert subject.respond_to?(m), "Not responding to ##{m}"
end
end

context "#truthy?" do
should "be true for string and integer representations of truth" do
assert "true".truthy?
assert "yes".truthy?
assert 1.truthy?
assert true.truthy?
end
end

context "#falsey?" do
should "be false for string and integer representations of false" do
assert "false".falsey?
assert "no".falsey?
assert 0.falsey?
assert false.falsey?
end
end

Expand Down

0 comments on commit eab17c0

Please sign in to comment.