Last active
December 18, 2022 21:41
-
-
Save blocknotes/94e9deb81a10bfc44fb28e39ebd3f9c0 to your computer and use it in GitHub Desktop.
Elixir like if
This file contains 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
def if(check, do:, else: nil) | |
result = check ? :do : :else | |
value = binding.local_variable_get(result) | |
value.is_a?(Proc) ? value.call : value | |
end | |
## Without block execution: | |
# def if(check, do:, else: nil) | |
# binding.local_variable_get(check ? :do : :else) | |
# end | |
self.if 5 < 10, do: 'Yes it is', else: 'Naaa' | |
self.if 5 < 10, do: -> { 'Yes with a block' }, else: 'Naaa' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment