From: Yasunari Momoi Date: 1997-09-18T09:00:31+09:00 Subject: [ruby-list:4382] BranchTable class ももちゃん@アルモニコスです. 必要に迫られて,BranchTable という (汚い (^^;)) クラスを書いて みました.もっとうまい方法があったら教えて下さい. class BranchTable def initialize(tbl) @__table = tbl @__debug = FALSE return super end def execute(__line) __i = 0 print "debug:", __line, "\n" if @__debug while __i < @__table.length / 2 if (__line =~ eval(@__table[2*__i])) return eval(@__table[2*__i + 1]) end __i = __i + 1 end end end ## ↓こんな感じで使う # tbl = BranchTable.new([ # '/^bye$/i', '0', # '/^put (.*)$/i', 'print $1, "\n"; 1', # '/^hoge/i', 'print $foo, "\n"; "HOGE"', # '/^(.*)$/i', 'print $foo, ":", $1, "\n"; 2', # ]) # $foo = 0 # while l = STDIN.gets.chop # $foo = $foo + 1 # case tbl.execute(l) # when 0 # break # when 1 # print "PUT CALLED\n" # when "HOGE" # print "HOGE CALLED\n" # end # end -- Yasunari Momoi , Armonicos Co., Ltd. WIDE: , JLUG: