-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added 0 and 0.0 to the list of things that are considered blank and w…
…rote specs to verify.
- Loading branch information
randy
committed
May 1, 2008
1 parent
8db6088
commit 0c98164
Showing
4 changed files
with
50 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,20 @@ | ||
module CoreExtensions | ||
class Object | ||
# An object is blank if it's nil, empty, or a whitespace string. | ||
# For example, "", " ", nil, [], and {} are blank. | ||
# | ||
# This simplifies | ||
# if !address.nil? && !address.empty? | ||
# to | ||
# if !address.blank? | ||
def blank? | ||
if respond_to?(:empty?) && respond_to?(:strip) | ||
empty? or strip.empty? | ||
elsif respond_to?(:empty?) | ||
empty? | ||
else | ||
!self | ||
end | ||
class Object | ||
# An object is blank if it's nil, empty, or a whitespace string. | ||
# For example, "", " ", nil, [], 0, 0.0 and {} are blank. | ||
# | ||
# This simplifies | ||
# if !address.nil? && !address.empty? | ||
# to | ||
# if !address.blank? | ||
def blank? | ||
if respond_to?(:empty?) && respond_to?(:strip) | ||
empty? or strip.empty? | ||
elsif respond_to?(:empty?) | ||
empty? | ||
elsif respond_to?(:zero?) | ||
zero? | ||
else | ||
!self | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
require File.dirname(__FILE__) + "/../lib/core_extensions" | ||
require 'spec' | ||
|
||
describe Object, "blank?" do | ||
it "should return true for 0" do | ||
0.should be_blank | ||
end | ||
|
||
it "should return true for 0.0" do | ||
0.0.should be_blank | ||
end | ||
|
||
it "should return false for 1" do | ||
1.should_not be_blank | ||
end | ||
|
||
it "should return true for ''" do | ||
''.should be_blank | ||
end | ||
|
||
it "should return false for 'moo'" do | ||
'moo'.should_not be_blank | ||
end | ||
|
||
it "should return true for nil" do | ||
nil.should be_blank | ||
end | ||
|
||
it "should return true for ' '" do | ||
' '.should be_blank | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters