All original content is created in Ukrainian. Not all content has been translated yet. Some posts may only be available in Ukrainian.Learn more

Why is an empty string in Ruby not false?

Post cover: Why is an empty string in Ruby not false?
Table of contentsClick link to navigate to the desired location
This content has been automatically translated from Ukrainian.
An empty string in Ruby is not false. Programmers like to compare this nuance with Perl, because in Perl an empty string is false.
It should be remembered that in Ruby the false values are only nil and false.
In Ruby, conditional expressions are evaluated based on the truthiness or falsiness of values. Of all possible values in Ruby, only two are false: nil and false. All other values, including an empty string (""), are considered true.
This means that when using an empty string in a conditional expression, it will always be treated as true. To check if a string is empty, several approaches can be used:

Method empty?

str = ""
puts "Empty string" if str.empty?

Empty string
=> nil
We see that str.empty? returned true and printed the response. Next, we see nil, but why - read more about it in this post.
Ruby has built-in methods that allow you to show whether a string is empty. And these methods should be used in conditional expressions to make the code as understandable as possible.

Comparison with an empty string

str = ""
puts "Empty string" if str == ""
Empty string
=> nil
Ruby is a cool language and allows you to do the same things in different ways. So if desired, you can use comparisons with an empty string in conditional expressions.
Checking the length of the string
str = ""
puts "Empty string" if str.size == 0
# or 
puts "Empty string" if str.length == 0
As I wrote - Ruby allows you to do the same things in different ways (note the size and length methods).

Why does an empty string return true?

It may seem illogical because '' is something that resembles nothing / emptiness. But let's delve a little into this.
In Ruby, it is easier to consider all objects, except for nil and false, as true. This reduces the number of specific cases that a programmer needs to remember. Ruby is built on the principle that everything is an object. This means that every value, including an empty string, must be an object and have certain behavior. Since an empty string is an object of the String class, it is considered true. By considering an empty string as true, Ruby simplifies conditional expressions and makes the code more predictable. For example, in conditional expressions, you can always rely on only nil and false being treated as false.
str = ""
if str
  puts "String is not nil or false"
else
  puts "String is nil or false"
end
By the way, let's prove that an object of the String class (specifically an empty string) is not an immediate value. An empty string (like any object of the String class) is not an immediate value, but is represented as an object in memory. In Ruby, only nil and false are false in conditional expressions. All other values, including an empty string, are considered true (truthy).
str1 = ""
str2 = ""
puts str1.object_id == str2.object_id

=> false # That is, each string has its own object_id 

str1.object_id
=> 46820

str2.object_id
=> 46800
So we have two different String objects with empty content. And the object returns true in conditional expressions.

This post doesn't have any additions from the author yet.

What is the purpose of the CVE (Common Vulnerabilities and Exposures) database?
29 May 08:05

What is the purpose of the CVE (Common Vulnerabilities and Exposures) database?

meme code
meme code@memecode
29 May 09:09

Which operating systems support Ruby?

meme code
meme code@memecode
Does Ruby create a new copy of an object when assigning a variable to another variable?
29 May 09:30

Does Ruby create a new copy of an object when assigning a variable to another variable?

meme code
meme code@memecode
What is the difference between immediate value and reference in Ruby?
29 May 12:00

What is the difference between immediate value and reference in Ruby?

meme code
meme code@memecode
Why does Ruby code return nil after executing puts?
29 May 20:30

Why does Ruby code return nil after executing puts?

meme code
meme code@memecode
What is the difference between nil and false in Ruby?
29 May 20:59

What is the difference between nil and false in Ruby?

meme code
meme code@memecode
The scope of a local variable in Ruby
03 Jun 16:46

The scope of a local variable in Ruby

meme code
meme code@memecode
What is the difference between int and bigint in Ruby? Minimum and maximum values.
13 Jun 06:37

What is the difference between int and bigint in Ruby? Minimum and maximum values.

meme code
meme code@memecode
What does the error 'is out of range' mean in Ruby on Rails? Range Error - Integer with a limit of 4 bytes
13 Jun 07:18

What does the error 'is out of range' mean in Ruby on Rails? Range Error - Integer with a limit of 4 bytes

meme code
meme code@memecode
What is immutability and mutability?
19 Jun 07:48

What is immutability and mutability?

meme code
meme code@memecode
What will be the result of adding 10.5 and 10?
23 Jun 13:23

What will be the result of adding 10.5 and 10?

meme code
meme code@memecode
[Ruby] What is the difference between variables that start with @, @@, and $?
23 Jun 14:00

[Ruby] What is the difference between variables that start with @, @@, and $?

meme code
meme code@memecode