Skip to content

Instantly share code, notes, and snippets.

@blocknotes
Last active December 18, 2022 21:41
Show Gist options
  • Save blocknotes/94e9deb81a10bfc44fb28e39ebd3f9c0 to your computer and use it in GitHub Desktop.
Save blocknotes/94e9deb81a10bfc44fb28e39ebd3f9c0 to your computer and use it in GitHub Desktop.
Elixir like if
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