tags:

views:

50

answers:

2

I was looking at this line:

    extensions << Module.new(&block) if block_given?

It seems to create a new module and add it to an arry.

Why would you build a module from a block though? The block could be anything and then the extensions array becomes unpredictable.

Edit: This is from Sinatra's base class:

  def register(*extensions, &block)
    extensions << Module.new(&block) if block_given?
    extensions.each do |extension|
      extend extension
      extension.registered(self) if extension.respond_to?(:registered)
    end
  end
+3  A: 

The code in a block is no more "unpredictable" than the code in a module made without using a block. Whether I write

module Foo
  def foo() "bar" end
end

or

Foo = Module.new do
  def foo() "bar" end
end

I get the same effect. Both allow you to extend another class's functionality, which is the purpose of this method.

Chuck
A: 

In this case it allows you to pass a &block inside oneliner, which makes code more readable.

Kirill Ishanov