Ruby DISCUSSION

Ruby nil?, empty? and blank?: which check do I need, and why is 0 truthy?

Started by anant Ruby nil checkempty vs blanktruthinessActive Support blanksafe navigation
4 replies 248 views 5 participants
Latest activity · 30 Sep 2026

Ruby nil?, empty? and blank?: which check do I need, and why is 0 truthy?

anant Ruby Forum
#1

I read lines from a serial port in a Ruby script and validate them before parsing. if line.empty? sometimes raises NoMethodError for nil, a line containing only spaces passes the check, and examples online use blank?, which does not exist when I run my script. I was also surprised that if count is true when count is 0.

What does each of these predicates test, which ones are plain Ruby, and what is a robust way to check for "no usable text"?

Community replies 4

Re: Ruby nil?, empty? and blank?: which check do I need, and why is 0 truthy?

#2

In Ruby only two values are falsy: nil and false. Everything else is truthy, including 0, 0.0, the empty string and the empty array. So if count only asks "is it neither nil nor false"; for the numeric test write if count > 0 or use count.zero?.

nil? is defined on every object and is true only for nil. empty? is defined on strings and containers (String, Array, Hash) and is true when the length is zero. nil has no empty? method, which is your NoMethodError.

Re: Ruby nil?, empty? and blank?: which check do I need, and why is 0 truthy?

#3

blank? and its opposite present? are not core Ruby. They come from Active Support, the utility library that ships with Rails, so they exist in a Rails app and not in a standalone script. blank? is true for nil, false, empty strings, strings containing only whitespace, and empty arrays and hashes. Note that 0.blank? is false.

You can use it outside Rails by installing the activesupport gem and adding require "active_support/core_ext/object/blank", but for a small script the plain Ruby equivalent is short enough.

Re: Ruby nil?, empty? and blank?: which check do I need, and why is 0 truthy?

#4

In plain Ruby, a robust "no usable text" test is line.nil? || line.strip.empty?. strip removes leading and trailing whitespace, including the \r\n that a serial line usually ends with, so a line of spaces or a bare line ending counts as empty. A shorter variant is line.to_s.strip.empty?, since nil.to_s is the empty string.

Be careful with the safe navigation operator here: line&.empty? returns nil when line is nil, which is falsy, so if line&.empty? treats a nil line as not empty, the opposite of what you want.

Re: Ruby nil?, empty? and blank?: which check do I need, and why is 0 truthy?

#5

It helps to know where nil comes from in I/O code. gets returns nil at end of input, and a read with a timeout may return nil or an empty string depending on the library, so check its documentation. A line that did arrive still carries its terminator; chomp removes a trailing newline or carriage return plus newline without touching other whitespace.

A compact read loop is while (line = port.gets), which stops at nil, followed by line = line.strip and next if line.empty?. After those two lines the rest of the loop body can assume a non-empty string.

TEP COMMUNITY