Actions
Feature #20326
closedAdd an `undefined` for use as a default argument.
Feature #20326:
Add an `undefined` for use as a default argument.
Status:
Feedback
Assignee:
-
Target version:
-
Description
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:
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:
A Ruby implementation might look like:
Actions