概要
メソッドの作成手順
前提
Confident Rubyではメソッド内の処理を次のように分類しています。
・Collecting Inputs(引数チェック、変換など)
・Performing Work(主処理)
・Delivering Results(戻り値に関わる処理)
・Handling Failure(例外処理)
当記事は上記のうち、Performing Work(主処理)に関する話です。
基本の手順
・メッセージの識別
・ロールの識別
・レシーバーの識別
サンプル仕様
Rubyのソースコードから単一行コメントを抽出して
「TODO:」コメントは todos.txt に
その他のコメントは others.txt に
出力する。
※前提として標準ライブラリ等については考えずにドメイン言語レベルでのみ考えます。
必要な処理を抽出
・ファイル名をもとにファイルのソースコードを読み取る
・単一行コメントでTODO: を含む行を抽出し、TODOコメントリストを返却する
・単一行コメントでTODO: を含まない行があれば、TODOコメントリストを返却する
・TODOコメントリストを todos.txt に出力する
・OTHERコメントリストを others.txt に出力する
メッセージの識別
・#read_source_code
・#find_todos
・#find_others
・#write_todos
・#write_others
レシーバーの識別
・ruby_source_code_file.read_source_code
・ruby_source_code.find_todo_comments
・ruby_source_code.find_other_comments
・todo_comment_file.write_todos
・other_comment_file.write_others
コードにする
※ここでは単純に用語をコードにしただけなので実行可能ではありません
def output_comment_files_from_ruby_source_code(filename) source_code = ruby_source_code_file.read_source_code(filename) todo_comments = ruby_source_code.find_todo_comments(source_code) other_comments = ruby_source_code.find_other_comments(source_code) todos_comment_file.write_todos(todo_comments) others_comment_file.write_others(other_comments) end