Skip to content

Instantly share code, notes, and snippets.

@blocknotes
Last active December 18, 2022 21:41
Show Gist options
  • Select an option

  • Save blocknotes/94e9deb81a10bfc44fb28e39ebd3f9c0 to your computer and use it in GitHub Desktop.

Select an option

Save blocknotes/94e9deb81a10bfc44fb28e39ebd3f9c0 to your computer and use it in GitHub Desktop.

Revisions

  1. blocknotes revised this gist Dec 18, 2022. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions elixir_if.rb
    Original file line number Diff line number Diff line change
    @@ -4,5 +4,10 @@ def if(check, do:, else: nil)
    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'
  2. blocknotes revised this gist Dec 18, 2022. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion elixir_if.rb
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,8 @@
    def if(check, do:, else: nil)
    binding.local_variable_get(check ? :do : :else)
    result = check ? :do : :else
    value = binding.local_variable_get(result)
    value.is_a?(Proc) ? value.call : value
    end

    self.if 5 < 10, do: 'Yes it is', else: 'Naaa'
    self.if 5 < 10, do: -> { 'Yes with a block' }, else: 'Naaa'
  3. blocknotes renamed this gist Dec 18, 2022. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. blocknotes created this gist Dec 18, 2022.
    5 changes: 5 additions & 0 deletions elixir_if
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    def if(check, do:, else: nil)
    binding.local_variable_get(check ? :do : :else)
    end

    self.if 5 < 10, do: 'Yes it is', else: 'Naaa'