Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

メタプログラミングRuby | 魔術 | コードプロセッサー

概要

コードプロセッサー

内容

コードプロセッサーとは外部ソースにあるコード文字列を処理する手法です。

サンプル

プロセッサー本体
# encoding: utf-8

class StreetFighter
  class << self
    alias zero instance_eval

    def 波動拳
      puts "236P"
    end

    def 昇竜拳
      puts "623P"
    end

    def 竜巻旋風脚
      puts "214K"
    end

    def 真空波動拳
      puts "236236P"
    end
  end
end

File.readlines("code_processor.txt", :encoding => Encoding::UTF_8).each do |line|
  StreetFighter.zero line
end
解析対象のコード文字列
波動拳
波動拳
昇竜拳
竜巻旋風脚
真空波動拳
実行結果
236P
236P
623P
214K
236236P