Tbpgr Blog

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

Ruby応用 | メタプログラミング

概要

メタプログラミングについて

メタプログラミングとは

プログラムをプログラムすること。
これにより、動的にクラスやメソッドや処理内容を定義することが可能です。

サンプル

動的にメソッドを定義するサンプル

# encoding: Windows-31J

class MetaSample
  define_method("hoge") do
    puts "hoge"
  end
end

meta_sample = MetaSample.new
meta_sample.hoge