Hacker News new | ask | show | jobs
by kaiuhl 250 days ago
Blocks are actually instances of the Proc class. There are helper methods to handle blocks passed to methods in a lightweight manner but you can also accept the block as a method argument, e.g.,

  class Integer
    def times(&blk)
      i = 0
      while i < self
        blk.call(i)
        i += 1
      end
    end
  end
1 comments

> Blocks are actually instances of the Proc class.

No, they aren't.

Blocks can be reified into instances of the Proc class, but they are not objects and reifying them into objects has overhead.

Using a & argument asks for a block passed to the method to be reified into a proc accessible in the body of the method, which is useful if you are going to do something with it that you can't do with a bare block.