Take the 2-minute tour ×

So I know in ruby that x.nil? will test if x is null.

What is the simplest way to test if x equals ' ', or ' '(two spaces), or ' '(three spaces), etc?

Basically, I'm wondering what the best way to test if a variable is all whitespace?

share|improve this question
let me actually clarify my question because it wasn't totally clear... im actually executing this in the context of an if statement. therefore im checking x.nil? and i also want x.(there is only whitespace here so i consider it nil) to both be conditionally evaluated –  user301752 Mar 25 '10 at 18:48
@user301752 when you say nil do you mean an empty string? They aren't the same in Ruby. (Open an irb session and check: x = ''; x.nil?.) –  Telemachus Mar 25 '10 at 19:37
why not trim it first? –  Eric Yin May 29 '12 at 3:29

7 Answers 7

up vote 9 down vote accepted
s =~ /^\s*$/

Regex solution. Here's a short ruby regex tutorial.

share|improve this answer
thanks! just started learning ruby and didnt realize that =~ would conditionally evaluate regular expression that is pretty sweet –  user301752 Mar 25 '10 at 18:56
@Stefan I think you want /^\s+$/ since the * will match too much (e.g., you get a match for an empty string - no spaces at all). –  Telemachus Mar 25 '10 at 19:39
I considered '' as all whitespace. This may or may not be correct in the domain of the problem, but I didn't think the question was worded in such a way to make either * or + incorrect. –  Stefan Kendall Mar 25 '10 at 20:00
Better to use anchors \A and \Z instead of ^ and $ -- the string "abc\n \ndef" will match your regex. –  glenn jackman Mar 25 '10 at 20:36

If you are using Rails, you can simply use:


This is safe to call when x is nil, and returns true if x is nil or all whitespace.

If you aren't using Rails you can get it from the activesupport gem. Install with gem install activesupport. In your file either require 'active_support/core_ext to get all active support extensions to the base classes, or require 'active_support/core_ext/string' to get just the extensions to the String class. Either way, the blank? method will be available after the require.

share|improve this answer
By the way I apologize to Rubyists for dragging Rails into what was otherwise a nice, pure Ruby discussion. It's just a nice pattern to emulate. –  MikeJ Mar 25 '10 at 19:15
@david-miani It's a shame there is no way I can +1 your edit, because it's an excellent one. It's a bad sort of over-optimizing to pluck code out of ActiveSupport when AS already supports including just the features you need. –  MikeJ Jan 29 at 1:26

Yet another :) string.all? { |c| c == ' ' }

share|improve this answer

If x is all whitespace, then x.strip will be the empty string. So you can do:

if not x.nil? and x.strip.empty? then
    puts "It's all whitespace!"

Alternatively, using a regular expression, x =~ /\S/ will return false if and only if x is all whitespace characters:

if not (x.nil? or x =~ /\S/) then
    puts "It's all whitespace!"
share|improve this answer
Why the downvote?! –  Mladen Jablanović Mar 25 '10 at 18:51
I didn't downvote. –  miorel Mar 25 '10 at 18:53
Mladen was referring to a downvote that was on your answer, not accusing you of downvoting others. –  Beanish Mar 25 '10 at 18:54
Right, sorry for not being clear. @miorel, you have +1 from me, both are cool ideas, you can even use !~ in the latter and get the boolean result straight. –  Mladen Jablanović Mar 25 '10 at 18:58
Strictly speaking /\S/ doesn't show you that a string is all whitespace. It determines whether there is anything in the string that isn't whitespace. This can sometimes matter - at least, if you consider an empty string to be different from a string that is all whitespace. –  Telemachus Mar 25 '10 at 19:48

"best" depends on the context, but here is a simple way.

share|improve this answer

Based on your comment I think you can extend the String class and define a spaces? method as follows:

$ irb
>> s = " "
=> " "
>> s.spaces?
NoMethodError: undefined method `spaces?' for " ":String
    from (irb):2
>> class String
>>     def spaces?
>>         x = self =~ /^\s+$/
>>         x == 0
>>     end
>> end
=> nil
>> s.spaces?
=> true
>> s = ""
=> ""
>> s.spaces?
=> false
share|improve this answer
a = "  " 

a.each_byte do |x|
  if x == 32
    puts "space"
share|improve this answer
To the downvoter, it would be good to know what's wrong with the answer ( or how is this not useful ) –  OscarRyz Mar 25 '10 at 18:48
I was not the downvoter, but this is neither the simplest solution, nor is checking for 32 going to catch all whitespace. –  miorel Mar 25 '10 at 18:52
The question was ambiguous about whether he wants spaces or all whitespace characters (including tabs, newlines, etc.). –  Chuck Mar 25 '10 at 19:01

Your Answer


