Project

nullz

0.0
No release in over 3 years
Low commit activity in last 3 years
Null Object Implementation
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 3.0
~> 2.0
>= 12.3.3
 Project Readme

Nullz 🔮

Build Status Code Climate Gem Version

A Ruby gem for elegantly handling nil values using the Null Object pattern.

Introduction 📖

Nullz provides a robust way to deal with nil values in Ruby. Instead of scattering nil checks throughout your code, Nullz allows you to handle these cases more gracefully with the NullObject pattern. This makes your code cleaner, more readable, and less prone to errors.

Installation 🔧

Add this line to your application's Gemfile:

gem 'nullz'

And then execute:

bundle install

Or install it yourself as:

gem install nullz

Usage 💡

Methods

  • _(obj): Returns obj unless it's nil, in which case it returns a Nullz::NullObject.

  • __(obj, on_null_object_created_proc = Proc.new {}): Similar to _, but also allows for a custom procedure when a NullObject is created.

  • safe(obj, on_null_object_created_proc = Proc.new {}): Toggles the use of NullObject based on the Nullz.use_null_object configuration.

Configuration

Configure Nullz in an initializer or similar:

Nullz.use_null_object = true
Nullz.on_null_object_created = -> { puts "Null object created!" }

NullObject Class

  • Represents nil or null.
  • This class defines a special kind of object meant to represent nil or null.
  • It overrides various methods to return either nil representations (like in to_s, to_i, etc.), or a new instance of NullObject (in arithmetic and bitwise operations).
  • It also defines behavior for comparison (==, !=) with nil and handling of unknown methods via method_missing.
  • Special methods like nil?, null?, empty? are overridden to provide appropriate behavior for a null object.