public
Fork of rails/rails
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/henrik/rails.git
Search Repo:
Add Object#nonblank? to ActiveSupport.

Similar to Numeric#nonzero? -- returns itself if not blank (per +blank?+), 
otherwise returns false.
henrik (author)
Mon Apr 14 02:48:44 -0700 2008
commit  29d81796084e110e37ff8a0bbc2647147ccb0449
tree    7209ca59256859715a627308a5bec25c17a2dc0e
parent  420c4b3d8878156d04f45e47050ddc62ae00c68c
...
12
13
14
 
 
 
 
 
 
 
 
 
 
 
 
 
15
16
17
...
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
0
@@ -12,6 +12,19 @@
0
   def blank?
0
     respond_to?(:empty?) ? empty? : !self
0
   end
0
+
0
+ # Similar to Numeric#nonzero? -- returns itself if not blank (per +blank?+),
0
+ # otherwise returns false. Simplifies e.g.
0
+ #
0
+ # name = params[:name].blank? ? "Default name" : params[:name]
0
+ #
0
+ # to
0
+ #
0
+ # name = params[:name].nonblank? || "Default name"
0
+ def nonblank?
0
+ !blank? && self
0
+ end
0
+
0
 end
0
 
0
 class NilClass #:nodoc:
...
16
17
18
 
 
 
 
 
 
 
19
...
16
17
18
19
20
21
22
23
24
25
26
0
@@ -16,5 +16,12 @@
0
     BLANK.each { |v| assert v.blank?, "#{v.inspect} should be blank" }
0
     NOT.each { |v| assert !v.blank?, "#{v.inspect} should not be blank" }
0
   end
0
+
0
+ def test_nonblank
0
+ x = " ".nonblank? || "blank"
0
+ assert_equal(x, "blank")
0
+ x = "nonblank".nonblank? || "blank"
0
+ assert_equal(x, "nonblank")
0
+ end
0
 end

Comments

    No one has commented yet.