トップ «前の日(03-03) 最新 次の日(03-05)» 追記   RSS 1.0 FEED  

Journal InTime


2014-03-04 (Tue)

_ 松江Ruby会議05予告

松江Ruby会議05でライブコーディングをすることになったが、「Module#prependとRefinementsで遊ぶ」というタイトルだけ出して、実は内容を考えてなかった。

以下のようなAspectJ風のコードを書くためのaspect.rbを実装しようと思う。

require "aspect"

class Job
  def run
    raise "subclass must override Command#execute"
  end
end

class PrintJob < Job
  def run
    puts "running PrintJob..."
  end
end

class BackupJob < Job
  def run
    puts "running BackupJob..."
  end
end

module Logging
  extend Aspect

  JOBS = execution(class: Job, method: :run)

  around JOBS do |join_point|
    STDERR.puts("enter #{join_point.signature}")
    begin
      join_point.proceed
    ensure
      STDERR.puts("exit #{join_point.signature}")
    end
  end
end

jobs = [PrintJob.new, BackupJob.new]
jobs.each(&:run)

実質20分くらいの持ち時間で上手く書けるかどうか。お楽しみに。

Tags: Ruby