1

In my app I want to prevent a variable from being blank — e.g., a user’s name should be something other than spaces and other whitespace. It will work like this:

foobar = gets.strip
arr = []
if # the string in the variable 'foobar' isn't blank
    arr.push(foobar)
end

Rails adds blank? method to class String, so that "".blank?, " ".blank?, and nil.blank? are all true. Ruby has similar empty? method, but it differs from blank?, as we can see in the example below (this won’t work in plain irb):

>> "      ".empty?
=> false
>> "      ".blank?
=> true

We see that a string of spaces is not empty, but it is blank.

Your solutions?

P.S. I gonna do this in pure Ruby, not in Rails.

3
  • 1
    Do you need something like if " " =~ /\A\s*\z/? Commented Oct 15, 2016 at 14:17
  • I need any checking facility. It can be method, statement, regex.
    – 0ll_Link0n
    Commented Oct 15, 2016 at 14:24
  • Ursus posted the same regex variation. Commented Oct 15, 2016 at 14:46

4 Answers 4

3

If you want something exactly like rails you could require activesupport. Or, if something simpler can go, you could do

class String
  BLANK_RE = /\A[[:space:]]*\z/

  def blank?
    BLANK_RE.match?(self)
  end
end

The regex is taken from rails.

1
  • As a note the ActiveSupport version also returns true for empty arrays, empty hashes and nil.
    – tadman
    Commented Oct 15, 2016 at 18:13
2

Better open the predefined String class and implement that method

class String
  def blank?
    if strip.empty?
      return true
    end
  end
end

and also NilClass

class NilClass
  def blank?
    return true
  end
end

Now you can use the blank? function in your code.

4
  • This results in blank? returning nil for a sting of one or more spaces. Commented Oct 15, 2016 at 15:40
  • @CarySwoveland Yes I made a mistake, Now I have called 'strip' method. self.strip.empty?, It works find now.
    – Gopal
    Commented Oct 15, 2016 at 15:53
  • 1
    Adding .self makes no difference as self is implied when omitted. Commented Oct 15, 2016 at 16:35
  • @CarySwoveland That's the great information for me, I am not aware of this, Thank you very much. Updated the answer now.
    – Gopal
    Commented Oct 15, 2016 at 16:47
2

In addition to Gopal's answer:

I would also use String.strip before checking on empty.

class String
    def blank? { self.strip.empty? }
end
0

Well, you can simply use ActiveRecord validation for that specific column (If we're discussing validation for model):

validates :name, presence: true

in the model- user.rb file.

The answers above are great, in case you wish to override/change the String class. but let's look at the specifications for these cases:

.empty? for STRING: Returns true if str has a length of zero.

.blank? for an OBJECT: An object is blank if it's false, empty, or a whitespace string. For example, false, '', ' ', nil, [], and {} are all blank.

written in the API.

Not the answer you're looking for? Browse other questions tagged or ask your own question.