Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Last active February 22, 2022 18:08
Show Gist options
  • Save JoshCheek/c16e4d3c7f3f4a0a804830e35e2cb1ec to your computer and use it in GitHub Desktop.
Save JoshCheek/c16e4d3c7f3f4a0a804830e35e2cb1ec to your computer and use it in GitHub Desktop.
Is there a best way to make this work?
module DefaultArgs
NOT_SET = Module.new
private def set?(value) = value != NOT_SET
private def not_set?(value) = !set?(value)
# This is the solution that I came up with:
# def self.extended(base)
# base.const_set :NOT_SET, NOT_SET
# super
# end
end
class OnInstance
include DefaultArgs
def call(a: NOT_SET) = set?(a)
end
class OnClass
extend DefaultArgs
def self.call(a: NOT_SET) = set?(a)
end
OnInstance.new.call # => false
OnInstance.new.call a: 1 # => true
OnClass.call # => false
OnClass.call a: 1 # => true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment