Actions
Feature #20326
closedAdd 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:
class Array
def count(item = undefined)
if item.undefined?
# ...
end
end
end
A Ruby implementation might look like:
module Kernel
UNDEFINED = Object.new
def UNDEFINED.inspect = -'undefined'
UNDEFINED.freeze
private_constant :UNDEFINED
def undefined? = self == UNDEFINED
module_function
def undefined = UNDEFINED
end
Actions
Like0
Like1Like0Like0Like0Like0Like0Like0