user508546
user508546

Reputation: 441

rails Object.blank? and whitespace

The rails documentation describes Object.blank? as such:

An object is blank if it’s false, empty, or a whitespace string. For example, "", " ", nil, [], and {} are blank.

def blank?
  respond_to?(:empty?) ? empty? : !self
end

(from: http://api.rubyonrails.org/classes/Object.html#method-i-blank%3F)

What I don't understand is how it's achieving the functionality of treating whitespace strings as blank.

" ".empty? returns false. Can anyone shed some light on this? Thanks.

Upvotes: 2

Views: 119

Answers (1)

Jeff Paquette
Jeff Paquette

Reputation: 7127

It's overridden for Strings:

From activesupport/core_ext/blank.rb

class String #:nodoc:
  def blank?
    self !~ /\S/
  end
end

Upvotes: 3

Related Questions