views:

173

answers:

7

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?

A: 
a = "  " 

a.each_byte do |x|
  if x == 32
    puts "space"
  end
end
Beanish
To the downvoter, it would be good to know what's wrong with the answer ( or how is this not useful )
OscarRyz
I was not the downvoter, but this is neither the simplest solution, nor is checking for 32 going to catch all whitespace.
miorel
The question was ambiguous about whether he wants spaces or all whitespace characters (including tabs, newlines, etc.).
Chuck
+3  A: 

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
Why the downvote?!
Mladen Jablanović
I didn't downvote.
miorel
Mladen was referring to a downvote that was on your answer, not accusing you of downvoting others.
Beanish
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ć
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
+2  A: 
s =~ /^\s*$/

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

Stefan Kendall
thanks! just started learning ruby and didnt realize that =~ would conditionally evaluate regular expression that is pretty sweet
@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
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
Better to use anchors `\A` and `\Z` instead of `^` and `$` -- the string `"abc\n \ndef"` will match your regex.
glenn jackman
A: 

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
+3  A: 

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

some_string.strip.empty?
jshen
+3  A: 

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 roll your own by copying the Rails source. Install Rails 2.3.5 then look at yourrubydir\lib\ruby\gems\1.8\gems\activesupport-2.3.5\lib\active_support\core_ext\object\blank.rb). The key bits are as follows:

class NilClass
  def blank?
    true
  end
end

class String
  def blank?
    self !~ /\S/
  end
end
MikeJ
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
A: 

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

Levi