Skip to content

Latest commit

 

History

History
40 lines (35 loc) · 806 Bytes

blocks_execute_around.md

File metadata and controls

40 lines (35 loc) · 806 Bytes

Blocks execute around

Example Logging

def with_logging(description)
  begin
    @logger.debug("Start #{description}")  
    yield
    @logger.debug("Completed #{description}")  
  rescue
    @logger.error("#{description} failed!")  
    raise
  end
end

# use it
with_logging("compute something") do
  25 * 26
end

In the wild:

Example Intitalization Block

class Document
  attr_accessor :title, :number
  def initialize
    yield( self ) if block_given?
  end
end

# use it
Document.new do |doc|
  doc.title  = "something"
  doc.number = 123
end