tags:

views:

830

answers:

9

Is there a better way than the following to check to see if a string is nil OR has a length of 0 in Ruby?

if my_string || my_string.length == 0
  return true
else
 return false

In C# there's the handy

string.IsNullOrEmpty(myString)

Anything similar to that in Ruby?

A: 

Every class has a nil? method:

if a_variable.nil?
    # the variable has a nil value
end

And strings have the empty? method:

if a_string.empty?
    # the string is empty
}

Remember that a string does not equal nil when it is empty, so use the empty? method to check if a string is empty.

yjerem
+4  A: 

nil? can be omitted in boolean contexts. Generally, you can use this to replicate the C# code:

return my_string.nil? || my_string.empty?
Konrad Rudolph
+3  A: 

Konrad Rudolph has the right answer.

If it really bugs you, monkey patch the String class or add it to a class/module of your choice. It's really not a good practice to monkey patch core objects unless you have a really compelling reason though.

class String
  def self.nilorempty?(string)
    string.nil? || string.empty?
  end
end

Then you can do String.nilorempty? mystring

jcoby
+9  A: 

An alternative to jcoby's proposal would be:

class NilClass
  def nil_or_empty?
    true
  end
end

class String
  def nil_or_empty?
    empty?
  end
end
Romulo A. Ceccon
ooh, even more tricky! I like it!
jcoby
+4  A: 

First of all, beware of that method:

As Jesse Ezel says:

Brad Abrams

"The method might seem convenient, but most of the time I have found that this situation arises from trying to cover up deeper bugs.

Your code should stick to a particular protocol on the use of strings, and you should understand the use of the protocol in library code and in the code you are working with.

The NullOrEmpty protocol is typically a quick fix (so the real problem is still somewhere else, and you got two protocols in use) or it is a lack of expertise in a particular protocol when implementing new code (and again, you should really know what your return values are)."

And if you patch String class... be sure NilClass has not been patch either!

class NilClass
    def empty?; true; end
end
VonC
+7  A: 

If you are willing to require ActiveSupport you can just use the #blank? method, which is defined for both NilClass and String.

Avdi
Note that a String containing only whitespace is still considered blank. So " ".blank? is true.
nertzy
+3  A: 

As it was said here before Rails (ActiveSupport) have a handy blank? method and it is implemented like this:

class Object
  def blank?
    respond_to?(:empty?) ? empty? : !self
  end
end

Pretty easy to add to any ruby-based project.

The beauty of this solution is that it works auto-magicaly not only for Strings but also for Arrays and other types.

Honza
+3  A: 

When I'm not worried about performance, I'll often use this:

if my_string.to_s == ''
  # It's nil or empty
end

There are various variations, of course...

if my_string.to_s.strip.length == 0
  # It's nil, empty, or just whitespace
end
Ezran
A: 

variable.blank? will do it. It returns true if the string is empty or if the string is nil.

satya