Search code examples
rubyconditional-statementstestingspaces

simplest way to check for just spaces in ruby


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?


Solution

  • s =~ /\A\s*\Z/
    

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