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.
if " " =~ /\A\s*\z/
?