Sha256: 59529f3ff0f46b204a344c8b2588d1d5f4b73fdce6c65fe4799e5492425e8437
Contents?: true
Size: 1.3 KB
Versions: 1
Compression:
Stored size: 1.3 KB
Contents
# +`nil` [![[version]](https://badge.fury.io/rb/null_plus.svg)](https://badge.fury.io/rb/null_plus) [![[ci]](https://github.com/janlelis/null_plus/workflows/Test/badge.svg)](https://github.com/janlelis/null_plus/actions?query=workflow%3ATest) 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?`](https://github.com/janlelis/null_question) 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**: ```ruby gem "null_plus" ``` ## Usage ```ruby 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: ```ruby 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.
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
null_plus-1.0.1 | README.md |