[ruby-core:95967] [Ruby master Feature#16374] Object#nullify to provide scalars with the effect similar to Enumerable#reject

From: shevegen@...
Date: 2019-11-26 20:50:19 UTC
List: ruby-core #95967
Issue #16374 has been updated by shevegen (Robert A. Heiler).


Personally I don't quite like method names that end in "fy alone. Crystal has .stringify
such as in **&.name.stringify** , which I always felt was a weird name - does not tell me much at
all. I have no particular opinion on the feature suggested itself, only on the name - although
the syntax example looks quite strange to me. Is it easy for others to decode on
**[].nullify(&:empty?)&.then(&:join)**? There seems to be so much information density packed 
in and it just doesn't "feel" like oldschool ruby. Ruby can be very simple. Somehow from the
functional side of things, either it is all quite complex ... or just so different that it
does not feel simple. Or just different - but to me it "feels" as if this all starts from a
higher complexity base. Perhaps a separate ruby language could be added, just to appease
more functional-in-style ruby users. ;)

May be interesting to hear what zverok thinks about the idea either way since he put 
forward ideas that are somewhat related to the suggestion here. :)

----------------------------------------
Feature #16374: Object#nullify to provide scalars with the effect similar to Enumerable#reject
https://bugs.ruby-lang.org/issues/16374#change-82803

* 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>

In This Thread

Prev Next