22

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?

user301752
  • 275
  • 1
  • 2
  • 7
  • 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

8 Answers8

36

If you are using Rails, you can simply use:

x.blank?

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.

David Miani
  • 14,518
  • 2
  • 47
  • 66
MikeJ
  • 1,164
  • 2
  • 8
  • 15
  • 7
    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 '14 at 01:26
28

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

some_string.strip.empty?
jshen
  • 11,507
  • 7
  • 37
  • 59
16
s =~ /\A\s*\Z/

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

dpk
  • 200
  • 9
Stefan Kendall
  • 66,414
  • 68
  • 253
  • 406
  • 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
  • 3
    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
  • 1
    @StefanKendall link is broken – BenKoshy Aug 27 '21 at 23:19
6

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!"
end

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!"
end
miorel
  • 1,863
  • 2
  • 14
  • 18
  • Mladen was referring to a downvote that was on your answer, not accusing you of downvoting others. – Beanish Mar 25 '10 at 18:54
  • 1
    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
0
a = "  " 

a.each_byte do |x|
  if x == 32
    puts "space"
  end
end
Beanish
  • 1,672
  • 9
  • 20
  • 2
    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
  • 2
    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
0

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
>> 
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
0

s.include?(" ")

Examples:

s = "A B C D"
s.include?(" ") #=> true

s = "ABCD"
s.include?(" ") #=> false
Breen ho
  • 1,601
  • 14
  • 23
-1

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

Levi
  • 4,628
  • 1
  • 18
  • 15