generated at
メタプログラミング
メタプログラミングとは
メタプログラミングとは、コードを記述するコードを記述すること

Wikipedia
> Metaprogramming is the writing of computer programs with the ability to treat programs as their data. It means that a program could be designed to read, generate, analyse and/or transform other programs, and even modify itself while running.

用途
RubyのActiveRecordでのメタプログラミング
シンプルな基底クラスを用意するだけで、DBのテーブル名、カラム名に合わせて勝手にメソッドを作ってくれる。
つまりDBを見てその名前に沿ったメソッド、自分で定義せずにすぐさま利用できる
rb
class Entity: attr_reader :table, :ident def initialize(table, ident) @table = table @ident = ident Database.sql "INSERT INTO #{@table} (id) VALUES (#{@ident})" end def set(col, val) Database.sql "UPDATE #{@table} SET #{col}='#{val}' WHERE id=#{@ident}" end def get(col) Database.sql ("SELECT #{col} FROM #{@table} WHERE id=#{@ident}")[0][0] end end # # これが # class Movie < Entity # def initialize(ident) # super("movies", ident) # end # def title # get("title") # end # def title=(value) # set("title", value) # end # def directory # get("directory") # end # def directory=(value) # set("directory", value) # end # end # こう書ける class Movie < ActiveRecord::Base end





コンパイル時と実行時
コンパイル時
変数や関数などの言語要素が存在する世界
実行時
マシンコードが存在する世界
コンパイル時の情報は失われている