Last active
February 22, 2022 18:08
-
-
Save JoshCheek/c16e4d3c7f3f4a0a804830e35e2cb1ec to your computer and use it in GitHub Desktop.
Is there a best way to make this work?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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