Ruby: empty? without rails

I want to get this test:

if (line.blank?) do_stuff 

... but I am in ruby, not in rails. What is the accepted idiom to achieve the same effect?

I am doing this for a line, where is testing for. Way? Isn't it the same as testing for .blank? (The entire line of spaces is not empty, but empty.)

+7
source share
5 answers

From ActiveSupport

 line !~ /[^[:space:]]/ 
+7
source

blank? defined not only in String, but also in the Nil part, because it is a double test. It checks if the variable is a nil? variable nil? or empty? /space.

You cannot specify a string if it is zero, because it does not have a nil? method nil? but can you ask nil? if he is nil? .

Active support has access to the main extensions that allow us to choose the necessary functionality and include the necessary methods. For blank? You can:

 require 'active_support/core_ext/object/blank' 

See blank? more details blank? and read the entire document to find out what else is available. Using Active Support, this method eliminates the need to download all ASs, but at the same time get useful features.

+8
source

It is possible:

 line.to_s.strip.empty? 
+4
source

blank? essentially just checking empty? || nil? empty? || nil? . Thus, you can either make an empty method yourself, or check both of them in conditional expressions.

+1
source
 if line.empty? || line.nil? 

Can you wrap it empty? method in your string class

+1
source

All Articles