今号の標準添付ライブラリ紹介では、汎用テンプレート エンジン ERB の紹介がありました。今回の宿題では、以下のメソッドによって似たようなテンプレート ベースの処理を作成してみましょう。
def expand(fmt, h)
# 文字列 fmt 中の部分文字列 "${v}" を h["v"] で置き換える。
end
テストケース:
require 'test/unit'
def expand(fmt, h)
# 文字列 fmt 中の部分文字列 "${v}" を h["v"] で置き換える。
end
class TestExpand < Test::Unit::TestCase
def test_expand1()
assert_equal("abc", expand("${v}bc", {'v'=>'a'}))
assert_equal("abc", expand("a${v}c", {'v'=>'b', 'b'=>'x'}))
assert_equal("abc", expand("${a}${b}${c}", {'a'=>'a', 'b'=>'b', 'c'=>'c'}))
end
end