Understand `nil` in Ruby

Search for a command to run...

No comments yet. Be the first to comment.
PostgreSQL Upgrade and Database Restoration Guide To support the pgvector gem, I upgraded my local Postgres.app from v13 to v17 on macOS. Below is the full upgrade and restoration process for my project. Environment OS: macOS PostgreSQL: Upgraded f...

Introduction In Ruby on Rails applications, handling database transactions efficiently is crucial to maintaining data integrity. One common issue developers face is dealing with runtime errors caused by unpersisted changes when using the with_lock me...

Environment MacOS Sonoma 14.3 Original Ruby version: 2.7.8 Ruby Version Manager: RVM 1.29.12 Goal Upgrade Ruby from 2.7.8 to 3.0.6. Issues Description After successfully installing Ruby 3.0.6, I encountered difficulties running bundle install in...

It is common to see require 'something' in a Rails application. But what exactly does require do? What is require used for in Ruby? require is used to load external libraries or modules into your program. For example, Before you require the JSON modu...

In my daily job, I mainly use Rails framework. To make code readable and maintainable, we seldom write raw SQL in the codebase. Instead, we use Rails's ActiveRecord::QueryMethods module which helps developers write beautiful queries quickly. For exam...

nil?nil is an instance of NilClass. It's a special Ruby object used to represent the absence of any value. And it also behaves like false when used in a conditional statement.
What's more, there is only one nil object, whose object_id is always 8 (in 64-bit Ruby).
# irb
$ nil.object_id
#=> 8
nil and nil?In Ruby, NilClass inherits from Object, and both of NilClass and Object define nil? method.
Only the object nil responds true to nil?
Other objects, like '' , {}, [] etc, responds false to nil?
# irb
$ nil.class
#=> NilClass < Object
$ NilClass.instance_methods.include?(:nil?)
#=> true
$ Object.instance_methods.include?(:nil?)
#=> true
For example,
# irb
$ nil.nil?
#=> true
$ ''.nil?
#=> false
$ [].nil?
#=> false
$ {}.nil?
#=> false
nil can only respond to blank? in Rails.It's common to see blank? in the codebase. However, blank? is only defined in Rails library, not in Ruby.
We can see the different results when calling nil.blank? in irb and Rails console.
For example,
# irb
$ nil.blank?
#=> Traceback (most recent call last):
# 4: from /Users/yuchu/.rvm/rubies/ruby-2.6.6/bin/irb:23:in `<main>'
# 3: from /Users/yuchu/.rvm/rubies/ruby-2.6.6/bin/irb:23:in `load'
# 2: from /Users/yuchu/.rvm/rubies/ruby-2.6.6/lib/ruby/gems/2.6.0/gems/irb-1.0.0/exe/irb:11:in `<top (required)>'
# 1: from (irb):14
#NoMethodError (undefined method `blank?' for nil:NilClass)
# rails console
$ nil.blank?
#=> true
nil and ! , !! operator! and !! are the operators to make sure the values return boolean.
$ !nil
#=> true
$ !!nil
#=> false
nil and Ruby if modifierThe syntax of Ruby if modifier looks like:
code if condition
if expressions are used for conditional execution. The values false and nil are false, and everything else is true.
Executes code if the conditional is true.
For example,
$ result = 'Hello'
$ note = { nickname: 'Jenny' }
$ result = 'Hello, Jane.' if nil
$ result
#=> 'Hello'
$ result = 'Hello, Jane.' if false
$ result
#=> 'Hello'
$ result = 'Hello, Jane.' if true
$ result
#=> 'Hello, Jane.'
$ result = 'Hello, Bob.' if note[:non_existent_key]
$ result
#=> 'Hello, Jane.'
$ result = 'Hello, Jenny.' if note[:nickname]
$ result
#=> 'Hello, Jenny.'
nil seems familiar, but there is something detailed worth learning.
Organizing the note helps me to correctly understand nil. It's helpful, especially when I need to read the code fastly.