Issue #16183 has been updated by shevegen (Robert A. Heiler).


sawa has it all covered. ;)

The explanation by matz is interesting.

"Use tap. Methods with side-effect should be handled with care. Making it chainable
has little benefit."

Personally I think other names, be these default_set, `Hash#default_proc_set or
with_default, do not completely correlate towards #tap, in my opinion; but I think
the problem is of special methods that may each have special meaning in different
parts of ruby. So from this point of view, unifying via .tap is simpler.

On the other hand, "tap" itself, at the least to me, conveys a slightly different
meaning than #with_default does, so I am not sure the two use cases overlap 100%.

Not sure if this would warrant the addition of a new syntax + idiom, and I am not
really pro/con either, so that could be discussed.

The complexity of method chains should be considered too, though. This may be
an individual's style, but these huge chains may impose a cognitive load to 
some ruby users possibly.

----------------------------------------
Feature #16183: Hash#with_default
https://bugs.ruby-lang.org/issues/16183#change-81749

* Author: zverok (Victor Shepelev)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
Reasons: there is no way, currently, to *declaratively* define Hash with default value (for example, to store it in constant, or use in an expression). Which leads to code more or less like this:


```ruby
FONTS = {
  title: 'Arial',
  body: 'Times New Roman',
  blockquote: 'Tahoma'
}.tap { |h| h.default = 'Courier' }.freeze

# Grouping indexes:
ary.each_with_object(Hash.new { |h, k| h[k] = [] }).with_index { |(el, h), idx| h[el.downcase] << idx }
```

With proposed method:
```ruby
FONTS = {
  title: 'Arial',
  body: 'Times New Roman',
  blockquote: 'Tahoma'
}.with_default('Courier').freeze

ary.each_with_object({}.with_default { [] }).with_index { |(el, h), idx| h[el.downcase] << idx }
```

About the block synopsys: I am not 100% sure, but I believe that _most_ of the time when `default_proc` provided, it looks like `{ |h, k| h[k] = some_calculation }`. So, I believe for this "declarative simplification" of defaults, it is acceptable to assume it as the only behavior (pass only key to block, and always store block's result); more flexible form would still be accessible with `Hash.new`.



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

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>