Project

null_plus

0.0
No commit activity in last 3 years
No release in over 3 years
This gem redefines Ruby's unary + operator to turn null objects into nil. By default, the unary + operator is not used by Ruby, so overloading it is not so dangerous as it might have sounded to you when you read it. Every object that returns true for null? is considered a null object.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

 Project Readme

+nil [version] [ci]

This gem redefines Ruby's unary + operator to turn null objects into nil. By default, the unary + operator is rarely¹ used by Ruby, so overloading it is not so dangerous as it might have sounded to you when you read it.

Every object that returns true for null? is considered a null object.

¹ (Ruby 2.3 introduced + for String: It will return an unfrozen version of the string)

Setup

Add to your Gemfile:

gem "null_plus"

Usage

class NullObject
  def null?
    true
  end
end

null = NullObject.new

+nil #=> nil
+null #=> nil
+"some object" #=> "some object"

# Useful for settings defaults or checking conditions:
if +null
  fail # will not run
end

value = +null || 42 #=> 42

Hint

Pay attention to proper operator precedence when chaining method class:

class NullObject
  def null?
    true
  end
end

null = NullObject.new

+null.class #=> NullObject
(+null).class #=> NilClass

J-_-L

Copyright (C) 2015 Jan Lelis https://janlelis.com. Released under the MIT license.