Skip to content

Instantly share code, notes, and snippets.

@Integralist
Created May 26, 2014 06:01
Show Gist options
  • Save Integralist/b79b7a7706cc150b457c to your computer and use it in GitHub Desktop.
Save Integralist/b79b7a7706cc150b457c to your computer and use it in GitHub Desktop.

Revisions

  1. Integralist created this gist May 26, 2014.
    20 changes: 20 additions & 0 deletions Pass block with zero arity.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    I saw this in some code written by BBC principle developer @kenoir and later located the following useful post: http://mudge.name/2011/01/26/passing-blocks-in-ruby-without-block.html

    ```ruby
    class Foo
    def initialize
    bar &Proc.new # voodoo
    end
    def bar(&block)
    block.call
    end
    end

    Foo.new { puts "hai" }
    ```

    In short, the reason it works is this:

    > If `Proc.new` is called from inside a method without any arguments of its own, it will return a new Proc containing the block given to its surrounding method.
    Very nice!