user4932805
user4932805

Reputation:

Ruby Block Invocation

Two examples in Ruby:

def invoke_block(&block)
  block.call
end

def invoke_block(&block)
  instance_eval(&block)
end

Why does instance_eval require the &?

Upvotes: 0

Views: 42

Answers (1)

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230306

Why does instance_eval require the &?

Because instance_eval accepts either a string or a block. It does not accept a Proc object (which is what name block is referring to, in this case). So you use operator & to "unpack" a proc into a block.

Upvotes: 4

Related Questions