Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

root/trunk/activesupport/lib/active_support/core_ext/blank.rb

Revision 9226, 0.7 kB (checked in by pratik, 1 year ago)

Improve documentation.

Line 
1 class Object
2   # An object is blank if it's false, empty, or a whitespace string.
3   # For example, "", "   ", +nil+, [], and {} are blank.
4   #
5   # This simplifies
6   #
7   #   if !address.nil? && !address.empty?
8   #
9   # to
10   #
11   #   if !address.blank?
12   def blank?
13     respond_to?(:empty?) ? empty? : !self
14   end
15 end
16
17 class NilClass #:nodoc:
18   def blank?
19     true
20   end
21 end
22
23 class FalseClass #:nodoc:
24   def blank?
25     true
26   end
27 end
28
29 class TrueClass #:nodoc:
30   def blank?
31     false
32   end
33 end
34
35 class Array #:nodoc:
36   alias_method :blank?, :empty?
37 end
38
39 class Hash #:nodoc:
40   alias_method :blank?, :empty?
41 end
42
43 class String #:nodoc:
44   def blank?
45     self !~ /\S/
46   end
47 end
48
49 class Numeric #:nodoc:
50   def blank?
51     false
52   end
53 end
Note: See TracBrowser for help on using the browser.