
Issue #20326 has been updated by shan (Shannon Skipper). zverok (Victor Shepelev) wrote in #note-6:
Unfortunately, such APIs are sometimes unavoidable, especially in implementing various generic algorithms (and not business logic).
I agree it's unfortunately unavoidable too often, so I resign myself to `default = UNDEFINED` or `default = default_not_set = true` like you mention above. zverok (Victor Shepelev) wrote in #note-6:
That being said, I am not sure I am in favor of having nil/undefined distinction in Ruby, and never proposed it myself (while considering it several times).
I'm not sold on `undefined` either, but would love some better way to detect default arguments that aren't set. Crimson on Ruby Discord suggested something similar to `block_given?` might be nice, like an `arg_given?(arg)`. ``` def fetch(item, default = nil) if arg_given?(:default) ``` ---------------------------------------- Feature #20326: Add an `undefined` for use as a default argument. https://bugs.ruby-lang.org/issues/20326#change-107158 * Author: shan (Shannon Skipper) * Status: Feedback ---------------------------------------- Variations around `UNDEFINED = Object.new` are a fairly common pattern to see used as default arguments to distinguish between `nil` and no argument provided. For example, a Ruby implementation of Array#count might look something like: ``` ruby class Array UNDEFINED = Object.new def UNDEFINED.inspect = 'UNDEFINED' UNDEFINED.freeze def count(item = UNDEFINED) if item == UNDEFINED # ... end end end ``` I'd like to propose adding an `undefined` module function method on Kernel to remove the boilerplate for this fairly common use case. An `__undefined__` method or `__UNDEFINED__` keyword would be alternatives to `undefined`. An `undefined?` helper would also be an optional nicety: ``` ruby class Array def count(item = undefined) if item.undefined? # ... end end end ``` A Ruby implementation might look like: ``` ruby module Kernel UNDEFINED = Object.new def UNDEFINED.inspect = -'undefined' UNDEFINED.freeze private_constant :UNDEFINED def undefined? = self == UNDEFINED module_function def undefined = UNDEFINED end ``` -- https://bugs.ruby-lang.org/