[ruby-core:95965] [Ruby master Feature#16374] Object#nullify to make for scalars the similar to effect of #reject for Enumerable

[email protected]
Newsgroups gmane.comp.lang.ruby.core
Message-ID <redmine.issue-16374.20191126161248.a780a31feaba969b@ruby-lang.org>
Issue #16374 has been reported by cvss (Kirill Vechera).

----------------------------------------
Feature #16374: Object#nullify to make for scalars the similar to effect of #reject for Enumerable
https://bugs.ruby-lang.org/issues/16374

* Author: cvss (Kirill Vechera)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
How about adding a new method to Object class?

```
class Object
	def nullify &block
		block.call(self) ? nil : self
	end
end

irb(main):139:0> 'asdf'.nullify(&:empty?)
=> "asdf"
irb(main):140:0> ''.nullify(&:empty?)
=> nil

```

It can be used together for chaining several methods with conditions. E.g. with &. operator and #then:

```
irb(main):154:0> [1, 2].nullify(&:empty?)&.then(&:join)
=> "12"
irb(main):155:0> [].nullify(&:empty?)&.then(&:join)
=> nil
irb(main):156:0> [].join
=> ""
irb(main):161:0> 'a b'.nullify(&:empty?)&.then(&:split)
=> ["a", "b"]
irb(main):162:0> ''.nullify(&:empty?)&.then(&:split)
=> nil
irb(main):163:0> ''.split
=> []

```

P.S. A similar opposite operation is available as a chain of ['then.detect'](https://ruby-doc.org/core-2.6.5/Object.html#method-i-then)



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.