<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	>

<channel>
	<title>FreeStyleVision</title>
	<atom:link href="http://freestyle.nvo.jp/feed" rel="self" type="application/rss+xml" />
	<link>http://freestyle.nvo.jp</link>
	<description>ネクストビジョン社員による情報配信を行っているブログです。</description>
	<pubDate>Mon, 14 May 2012 00:30:06 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7.1</generator>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>「性別」列のインデックス付与について</title>
		<link>http://freestyle.nvo.jp/archives/1041</link>
		<comments>http://freestyle.nvo.jp/archives/1041#comments</comments>
		<pubDate>Mon, 14 May 2012 00:30:06 +0000</pubDate>
		<dc:creator>yasukuni</dc:creator>
		
		<category><![CDATA[技術情報]]></category>

		<category><![CDATA[インデックス]]></category>

		<category><![CDATA[データベース]]></category>

		<guid isPermaLink="false">http://freestyle.nvo.jp/?p=1041</guid>
		<description><![CDATA[データベースのインデックスを張る際によく「性別」に付けても意味が無いと言われたりしますが、実際の所どうなのでしょうか？(SQL Serverで)試してみました。
で。結論から言うと遅くなります。(というか使用されません) [...]]]></description>
			<content:encoded><![CDATA[<p>データベースのインデックスを張る際によく「性別」に付けても意味が無いと言われたりしますが、実際の所どうなのでしょうか？(SQL Serverで)試してみました。</p>
<p>で。結論から言うと遅くなります。(というか使用されません) ただし...</p>
<p><span id="more-1041"></span></p>
<h4>1.サンプルデータ作成</h4>
<p>まず、データを準備します。100万件のサンプルデータを用意し、"sex"カラムを性別と見立て(0=男性, 1=女性)ランダムに設定します。</p>
<div class="igBar"><span id="lsql-6"><a href="#" onclick="javascript:showPlainTxt('sql-6'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">SQL:</span>
<div id="sql-6">
<div class="sql">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">/*テーブル作成*/</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> users<span style="color:#006600; font-weight:bold;">&#40;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">id&nbsp; &nbsp; &nbsp; &nbsp;INT IDENTITY<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #cc66cc;color:#800000;">1</span>,<span style="color: #cc66cc;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">,name&nbsp; &nbsp; NCHAR<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #cc66cc;color:#800000;">20</span><span style="color:#006600; font-weight:bold;">&#41;</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">NULL</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">,sex&nbsp; &nbsp; &nbsp;TINYINT&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">NULL</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">,age&nbsp; &nbsp; &nbsp;TINYINT&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">NULL</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">,address NCHAR<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #cc66cc;color:#800000;">50</span><span style="color:#006600; font-weight:bold;">&#41;</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">NULL</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">,tel&nbsp; &nbsp; &nbsp;NCHAR<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #cc66cc;color:#800000;">15</span><span style="color:#006600; font-weight:bold;">&#41;</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">NULL</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">,CONSTRAINT PK_USERS <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> CLUSTERED <span style="color:#006600; font-weight:bold;">&#40;</span>id <span style="color: #993333; font-weight: bold;">ASC</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">/*ダミーデータ作成*/</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">DECLARE @CNT int</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333; font-weight: bold;">SET</span> @CNT = <span style="color: #cc66cc;color:#800000;">0</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">WHILE @CNT &lt;<span style="color: #cc66cc;color:#800000;">1000000</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">BEGIN</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> users<span style="color:#006600; font-weight:bold;">&#40;</span>name, sex, age, address, tel<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span><span style="color:#006600; font-weight:bold;">&#40;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #ff0000;">'スズキ イチロウ'</span> + CAST<span style="color:#006600; font-weight:bold;">&#40;</span>@CNT <span style="color: #993333; font-weight: bold;">AS</span> VARCHAR<span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">,FLOOR<span style="color:#006600; font-weight:bold;">&#40;</span>RAND<span style="color:#006600; font-weight:bold;">&#40;</span>CONVERT<span style="color:#006600; font-weight:bold;">&#40;</span>INT, CONVERT<span style="color:#006600; font-weight:bold;">&#40;</span>VARBINARY<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #cc66cc;color:#800000;">4</span><span style="color:#006600; font-weight:bold;">&#41;</span>, NEWID<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> * <span style="color: #cc66cc;color:#800000;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">,FLOOR<span style="color:#006600; font-weight:bold;">&#40;</span>RAND<span style="color:#006600; font-weight:bold;">&#40;</span>CONVERT<span style="color:#006600; font-weight:bold;">&#40;</span>INT, CONVERT<span style="color:#006600; font-weight:bold;">&#40;</span>VARBINARY<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #cc66cc;color:#800000;">4</span><span style="color:#006600; font-weight:bold;">&#41;</span>, NEWID<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> * <span style="color: #cc66cc;color:#800000;">80</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">,<span style="color: #ff0000;">'東京都千代田区1-1-1'</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">,<span style="color: #ff0000;">'03-1234-5678'</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333; font-weight: bold;">SET</span> @CNT = @CNT + <span style="color: #cc66cc;color:#800000;">1</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">END</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">/*性別の分布を確認*/</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333; font-weight: bold;">SELECT</span> COUNT<span style="color:#006600; font-weight:bold;">&#40;</span>*<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color: #993333; font-weight: bold;">FROM</span> users</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333; font-weight: bold;">SELECT</span> sex, COUNT<span style="color:#006600; font-weight:bold;">&#40;</span>*<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color: #993333; font-weight: bold;">FROM</span> users <span style="color: #993333; font-weight: bold;">GROUP</span> <span style="color: #993333; font-weight: bold;">BY</span> sex </div>
</li>
</ol>
</div>
</div>
</div>
<p>
<img class="alignnone size-full wp-image-1060" title="サンプルデータ - 性別分布" src="http://freestyle.nvo.jp/wp-content/uploads/2012/05/e382ade383a3e38397e38381e383a3.png" alt="サンプルデータ - 性別分布" width="134" height="119" /></p>
<h4>2.インデックス付与前</h4>
<p>それでは、データの準備が出来たので、インデックスなしの状態でクエリを流してみます。</p>
<div class="igBar"><span id="lsql-7"><a href="#" onclick="javascript:showPlainTxt('sql-7'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">SQL:</span>
<div id="sql-7">
<div class="sql">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">DBCC DROPCLEANBUFFERS&nbsp; <span style="color: #808080; font-style: italic;">/*clear cache*/</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">DBCC FREEPROCCACHE&nbsp; &nbsp; &nbsp;<span style="color: #808080; font-style: italic;">/*clear cache*/</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333; font-weight: bold;">SELECT</span> * <span style="color: #993333; font-weight: bold;">FROM</span> users <span style="color: #993333; font-weight: bold;">WHERE</span> sex = <span style="color: #cc66cc;color:#800000;">0</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p>
[インデックス付与前 - 実行計画]<br />
<img class="alignnone size-full wp-image-1062" title="インデックス付与前 - 実行計画" src="http://freestyle.nvo.jp/wp-content/uploads/2012/05/1.png" alt="インデックス付与前 - 実行計画" width="344" height="237" /></p>
<p>[インデックス付与前 - 実行時間]<br />
<img class="alignnone size-full wp-image-1066" title="インデックス付与前 - 実行時間" src="http://freestyle.nvo.jp/wp-content/uploads/2012/05/21.png" alt="インデックス付与前 - 実行時間" width="520" height="99" /></p>
<h4>3.インデックス付与後</h4>
<p>次にインデックスを付与した状態でクエリを流します。<br />
(何か切ないですが、そのままだとインデックスは使用されないのでテーブルヒントを与えます。。。)</p>
<div class="igBar"><span id="lsql-8"><a href="#" onclick="javascript:showPlainTxt('sql-8'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">SQL:</span>
<div id="sql-8">
<div class="sql">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">/*インデックスの作成*/</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">INDEX</span> IDX_USERS_SEX <span style="color: #993333; font-weight: bold;">ON</span> users<span style="color:#006600; font-weight:bold;">&#40;</span>sex<span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">DBCC DROPCLEANBUFFERS&nbsp; <span style="color: #808080; font-style: italic;">/*clear cache*/</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">DBCC FREEPROCCACHE&nbsp; &nbsp; &nbsp;<span style="color: #808080; font-style: italic;">/*clear cache*/</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333; font-weight: bold;">SELECT</span> * <span style="color: #993333; font-weight: bold;">FROM</span> users <span style="color: #993333; font-weight: bold;">WITH</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #993333; font-weight: bold;">INDEX</span> = IDX_USERS_SEX<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color: #993333; font-weight: bold;">WHERE</span> sex = <span style="color: #cc66cc;color:#800000;">0</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p>
[インデックス付与後 - 実行計画]<br />
<img class="alignnone size-full wp-image-1107" title="インデックス付与後 - 実行計画" src="http://freestyle.nvo.jp/wp-content/uploads/2012/05/e382ade383a3e38397e38381e383a32.png" alt="インデックス付与後 - 実行計画" width="451" height="241" /></p>
<p>[インデックス付与後 - 実行時間]<br />
<img class="alignnone size-full wp-image-1069" title="インデックス付与後 - 実行時間" src="http://freestyle.nvo.jp/wp-content/uploads/2012/05/4.png" alt="インデックス付与後 - 実行時間" width="514" height="99" /></p>
<h4>4.理由</h4>
<p>ここまで、茶番劇のようですが確かに遅くなりました。なぜでしょうか？<br />
よくインデックスは書籍の索引に例えられますが今回作成したインデックスを例えるとこうなるはずです。</p>
<hr />索引 - 性別<br />
男性・・・1, 3, 6, 10<br />
女性・・・2, 4, 5, 7, 8, 9</p>
<hr />もし、人がこの本の男性を探すというのであれば、100万ページをめくる作業が約1/2に絞り込めるのでこの索引は非常に有用だと思います。しかしRDBMSでは効率的にデータを読み込む為に数ページをまとめてディスクから読み込みます。（まとめて読み込むサイズは「ブロックサイズ」「ページサイズ」などと呼ばれます）</p>
<p>本で例えると1,3,6,10のページをめくる為には必然的に（ブロックサイズを5ページと仮定）1～10ページをめくっているのと変わらないことになります。そこでRDBMSはこう考えます「結局全部めくるんなら、索引とか開くの面倒だからインデックス使うのや～めた」と。</p>
<h4>5.本のページをめくる必要がない場合</h4>
<p>ここまでで、「性別」にインデックスを付与しても意味がない(使われない。使っても+αの処理が発生するため遅くなる)の理由ですが<strong>例外</strong>があります。それは、本のページをめくる必要がない場合です。</p>
<p>例えば、件数を取得するだけなら人もRDBMSも同じで索引の男性に書かれているページ番号を数えるだけで事足ります。仮にインデックスがない場合(全データ走査)と比較すると100万件で約25倍の差が出ました。</p>
<div class="igBar"><span id="lcode-9"><a href="#" onclick="javascript:showPlainTxt('code-9'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-9">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000;">/*男性の件数を取得*/</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">SELECT COUNT<span style="color:#006600; font-weight:bold;">&#40;</span>*<span style="color:#006600; font-weight:bold;">&#41;</span> FROM users WHERE sex = <span style="color:#800000;color:#800000;">0</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p>
[インデックス付与前後 - 件数取得(左:付与後, 右:付与前)]<br />
<img class="alignnone size-full wp-image-1079" title="インデックス付与前後 - 件数取得" src="http://freestyle.nvo.jp/wp-content/uploads/2012/05/5.png" alt="インデックス付与前後 - 件数取得" width="484" height="99" /></p>
<h4>6.本のページをめくらせない</h4>
<p>結局、本のページをめくらなければ(データへのアクセスがなく、インデックスで事足りる場合)「性別」のインデックスは有用だということが解りました。だたし、性別毎の平均年齢を調べようものなら、やはり全てのページをめくる必要があります。(「4.理由」の通りインデックスは使われない)</p>
<p>そこで、ページをめくらないようにする方法に<strong>カバーリングインデックス</strong>と呼ばれるSELECTやWHEREに含まれるフィールドを全て網羅(カバー)するものがあります。「インデックスに年齢も含めてしまえば事足りるよね」の考えで本の索引で例えるとこのようなものです。</p>
<hr />索引 - 性別<span style="color:#F22">(年齢情報付き)</span><br />
男性・・・1<span style="color:#F22">(15)</span>, 3<span style="color:#F22">(23)</span>, 6<span style="color:#F22">(9)</span>, 10<span style="color:#F22">(45)</span><br />
女性・・・2<span style="color:#F22">(7)</span>, 4<span style="color:#F22">(35)</span>, 5<span style="color:#F22">(16)</span>, 7<span style="color:#F22">(63)</span>, 8<span style="color:#F22">(27)</span>, 9<span style="color:#F22">(30)</span></p>
<hr />インデックスが大きくなった分I/Oが増えた為、先ほど同様25倍と言えませんが、インデックスがない場合と比べて10倍以上高速です。(サンプルデータはフィールド数が少ないので...実際はもう少し有益なはずです。)</p>
<div class="igBar"><span id="lsql-10"><a href="#" onclick="javascript:showPlainTxt('sql-10'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">SQL:</span>
<div id="sql-10">
<div class="sql">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">/*カバーリングインデックス*/</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">INDEX</span> IDX_USERS_SEX_WITH_AGE <span style="color: #993333; font-weight: bold;">ON</span> users<span style="color:#006600; font-weight:bold;">&#40;</span>sex<span style="color:#006600; font-weight:bold;">&#41;</span> INCLUDE<span style="color:#006600; font-weight:bold;">&#40;</span>age<span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333; font-weight: bold;">SELECT</span> sex, AVG<span style="color:#006600; font-weight:bold;">&#40;</span>age<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color: #993333; font-weight: bold;">FROM</span> users <span style="color: #993333; font-weight: bold;">GROUP</span> <span style="color: #993333; font-weight: bold;">BY</span> sex </div>
</li>
</ol>
</div>
</div>
</div>
<p>
[カバーインデックス付与前後 - 性別毎平均年齢(左:付与後, 右:付与前)]<br />
<img class="alignnone size-full wp-image-1096" title="カバーリングインデックス付与前後" src="http://freestyle.nvo.jp/wp-content/uploads/2012/05/e382ade383a3e38397e38381e383a31.png" alt="カバーリングインデックス付与前後" width="482" height="99" /></p>
<p>ということで、「性別」のインデックスは一概に意味がないとは言えませんが、インデックスを付与するとデータサイズの肥大化や更新処理の速度低下を招くため用途に応じた適切なインデックスが必要です。</p>
<p>また、今回一般的なB-Treeインデックスについて記載しましたが、ガーディナリティが低い(性別、各種フラグ、カテゴリの様にデータの種類が少ない)フィールドには「ビットマップインデックス」という種類のインデックスが効果的な様です。</p>
]]></content:encoded>
			<wfw:commentRss>http://freestyle.nvo.jp/archives/1041/feed</wfw:commentRss>
		</item>
		<item>
		<title>vimのちょっと便利な小技（４）</title>
		<link>http://freestyle.nvo.jp/archives/1030</link>
		<comments>http://freestyle.nvo.jp/archives/1030#comments</comments>
		<pubDate>Fri, 20 Apr 2012 04:45:20 +0000</pubDate>
		<dc:creator>fujii</dc:creator>
		
		<category><![CDATA[技術情報]]></category>

		<guid isPermaLink="false">http://freestyle.nvo.jp/?p=1030</guid>
		<description><![CDATA[今回は『選択範囲のテキストを削除』をご紹介しようと思います。
ビジュアルモード中に"d"を押すことで選択範囲のテキストを削除する事ができます。
以下の様なテキストがあったとします。
下記の文章から３行ほど削除したい場合、 [...]]]></description>
			<content:encoded><![CDATA[<p>今回は『選択範囲のテキストを削除』をご紹介しようと思います。<br />
ビジュアルモード中に"d"を押すことで選択範囲のテキストを削除する事ができます。</p>
<p>以下の様なテキストがあったとします。<br />
下記の文章から３行ほど削除したい場合、vi使いの方なら、該当行にカーソルを移動し、「3dd」と入力します。</p>
<p><img class="alignnone size-full wp-image-1032" title="vim_tips4_1" src="http://freestyle.nvo.jp/wp-content/uploads/2012/04/vim_tips4_1.png" alt="vim_tips4_1" width="238" height="170" /></p>
<p><span id="more-1030"></span></p>
<p>ビジュアルモードが利用可能なvimの場合、以下の様にする事で視覚的にもわかりやすく削除する事が可能です。</p>
<p>1. 「shift + v」行選択モードにします。</p>
<p><img class="alignnone size-full wp-image-1033" title="vim_tips4_2" src="http://freestyle.nvo.jp/wp-content/uploads/2012/04/vim_tips4_2.png" alt="vim_tips4_2" width="214" height="248" /></p>
<p>2. 「j」「k」で範囲選択します。</p>
<p><img class="alignnone size-full wp-image-1034" title="vim_tips4_3" src="http://freestyle.nvo.jp/wp-content/uploads/2012/04/vim_tips4_3.png" alt="vim_tips4_3" width="188" height="172" /></p>
<p>3. 「d」押下で、選択範囲を削除する事が可能です。</p>
<p>これを応用し、もう少し素早く特定範囲のテキストを削除する事もできます。<br />
今回はJavaScriptのfunction 「hoge」を削除したいと思います。</p>
<p>1. カーソルをhoge末尾の「{」に移動します。</p>
<p><img class="alignnone size-full wp-image-1035" title="vim_tips4_4" src="http://freestyle.nvo.jp/wp-content/uploads/2012/04/vim_tips4_4.png" alt="vim_tips4_4" width="185" height="178" /></p>
<p>2. 「shift + v」行選択モードにします。</p>
<p>3. 「%(shift + 5)」押下します。</p>
<p><img class="alignnone size-full wp-image-1036" title="vim_tips4_5" src="http://freestyle.nvo.jp/wp-content/uploads/2012/04/vim_tips4_5.png" alt="vim_tips4_5" width="190" height="174" /></p>
<p>「{」に対応する「}」にカーソルが移動します。<br />
行選択モード上記の操作を行う事で、「{」から「}」までを選択する事ができます。</p>
<p>4. 「d」押下で、選択範囲を削除する事が可能です。</p>
<p>以上、ちょっと便利な小技（４）でした</p>
]]></content:encoded>
			<wfw:commentRss>http://freestyle.nvo.jp/archives/1030/feed</wfw:commentRss>
		</item>
		<item>
		<title>enchant.jsのサンプルゲームを読む</title>
		<link>http://freestyle.nvo.jp/archives/1020</link>
		<comments>http://freestyle.nvo.jp/archives/1020#comments</comments>
		<pubDate>Wed, 04 Apr 2012 04:28:31 +0000</pubDate>
		<dc:creator>山平</dc:creator>
		
		<category><![CDATA[技術情報]]></category>

		<category><![CDATA[HTML5]]></category>

		<category><![CDATA[JavaScript]]></category>

		<category><![CDATA[オープンソース]]></category>

		<category><![CDATA[開発環境]]></category>

		<guid isPermaLink="false">http://freestyle.nvo.jp/?p=1020</guid>
		<description><![CDATA[数回に渡ってenchant.jsとゲームについて、周辺の話を進めてきましたので、そろそろ本題に入ります。
enchant.jsにサンプルとして同梱されているアクションゲームのソースを眺めながら、少し手を入れてみます。

 [...]]]></description>
			<content:encoded><![CDATA[<p>数回に渡ってenchant.jsとゲームについて、周辺の話を進めてきましたので、そろそろ本題に入ります。<br />
enchant.jsにサンプルとして同梱されているアクションゲームのソースを眺めながら、少し手を入れてみます。</p>
<ul>
<li><a href="http://freestyle.nvo.jp/archives/982">enchant.jsの概説</a></li>
<li><a href="http://freestyle.nvo.jp/archives/986">GIMPでドット絵を加工する</a></li>
<li><a href="http://freestyle.nvo.jp/archives/1006">ゲームで使う数字の遊び方</a></li>
</ul>
<p><span id="more-1020"></span></p>
<h3>準備</h3>
<h4>解説用ファイル</h4>
<p>以下のファイルをダウンロードしてください。<br />
今回の解説用に手を加えたソースファイルです。</p>
<p>【添付ファイル】<a href="http://freestyle.nvo.jp/wp-content/uploads/2012/03/enchant.zip">enchant.zip</a></p>
<p>添付ファイルの内容</p>
<dl>
<dt>/enchant.js</dt>
<dd>enchantエンジン本体です。(今回は触れません)</dd>
<dt>/plugins</dt>
<dd>利用しているプラグインファイルが入っています。(今回は触れません)</dd>
<dt>/examples/action/index.html<br />
/examples/action/game.js</dt>
<dd>今回参考にするサンプルのオリジナルです</dd>
<dt>/examples/action/index.comment.html<br />
/examples/action/game.comment.js</dt>
<dd>今回参考にするサンプルにコメントのみを加えたファイルです。</dd>
<dt>/examples/action/index.fix.html<br />
/examples/action/game.fix.js</dt>
<dd>今回参考にするサンプルを改造したファイルです。</dd>
</dl>
<p>/examples/action配下には上で説明しているファイル以外が含まれています。<br />
これらはゲームやプラグインから呼び出されるものなので、今のところ気にしないでください。</p>
<h4>ドキュメント</h4>
<p>以下のドキュメントが読めるようになれば、enchant.jsを使ったゲーム開発で困ることはないと思います。</p>
<dl>
<dt><a href="http://enchantjs.com/ja/reference.html">Reference</a></dt>
<dd>enchant.jsを大まかに知るのに適しています。<br />
私の解説が足りないとき、こちらを参考にすると良いと思います。</dd>
<dt><a href="http://wise9.github.com/enchant.js/doc/ja/">Class Index</a></dt>
<dd>enchant.jsの各クラスに関する詳細です。</dd>
</dl>
<h3>オリジナルソースの解説</h3>
<p>オリジナルのソースコードにコメントのみを追加した添付ファイルを元に解説します。<br />
以降、特に断りなくコードと言う場合は[/examples/action/game.comment.js]を指してます。</p>
<p>enchant.jsは、ツリー状にゲームオブジェクトを保持しています。<br />
Game-Scene-Node(の派生オブジェクト)という階層を作った上でGameオブジェクトのstartメソッドを呼び出します。<br />
各オブジェクトはenterframeイベントを持っており、パラパラ漫画のようにゲーム中の各コマで実行されます。<br />
キャラクタの動きやスクロール、ゲームオーバーの判定などはそれぞれのオブジェクトのenterframeイベントで処理されます。</p>
<p>今回の解説にアクションゲームを選んだのは、ソースコードが一番読みやすいからです。<br />
上から読んでいけば理解できる順序でコーディングされています。</p>
<p>基本的な流れは</p>
<ol>
<li>Gameオブジェクトの宣言と設定</li>
<li>Gameオブジェクトに追加する各オブジェクトの宣言と設定、enterframeイベントの実装（の繰り返し）</li>
<li>ゲーム開始</li>
</ol>
<p>というシンプルなものですが、2点ほど注意が必要です。<br />
ひとつはスコープ、もう一つはクロージャです。</p>
<h4>スコープ</h4>
<p>ほぼすべての処理がイベントとして定義するFunctionの宣言として記述されています。<br />
「window.onload→game.onload→bear.enterframe」といったイベント用のFunction中にさらにイベント用のFunctionが定義されています。<br />
コード中に各スコープの開始と終了をコメントしています。<br />
各スコープの中での「this」が何を指しているか、混乱しないように注意が必要です。</p>
<h4>クロージャ</h4>
<p><a href="http://ja.wikipedia.org/wiki/%E3%82%AF%E3%83%AD%E3%83%BC%E3%82%B8%E3%83%A3">クロージャ - Wikipedia</a></p>
<p>enterframeイベント内のコードを読んでいると、イベントスコープの外で宣言されているgameやbearが普通に出てきます。<br />
これがいわゆるクロージャで、gameやbearが宣言されたスコープ(onloadイベントなど)を外れた後にも参照され続けます。</p>
<p>当然gameやbearが宣言される前に参照できないので、コードを整理する際に順番を間違えて動かなくなったりします。<br />
場合によっては便利なのですが、何でもかんでもクロージャで解決していると、どんどん可読性が落ちて行きます。<br />
まずはきちんとオブジェクト構造を整えることが大切です。</p>
<h3>改造ソースの解説</h3>
<p>ここまでの説明と公式のドキュメントでオリジナルのコードは大体読めると思います。<br />
添付の[/examples/action/game.fix.js]にも少し触れます。</p>
<p>オリジナルのアクションゲームは読みやすい半面、これを元に規模を大きくしていくには向いていない部分があります。<br />
ある程度の規模のプログラムになってくると、別の読みやすさが必要になってきます。<br />
オブジェクトの定義と利用がキレイに分かれている方がゲーム全体の流れを追いやすくなるのです。</p>
<p>サンプルソースを読みにくくさせているのは他の部分に比べて極端に長いクマの定義とマップデータですので、これを外に出します。<br />
ついでなのでRectangleオブジェクトも外に出してしまいます。<br />
ここで注目したいのは長い宣言を外に出したおかげでgame.onloadイベント内がすっきりして読みやすくなったことです。</p>
<p>鍵を持っていれば扉が開く、といったゲーム中の出来事をわかりやすく記述するために、それ以外のコードはそれ以外の場所に記述すると良いと思います。</p>
<h3>次に読むべきコード</h3>
<p>enchant.jsを使ったゲームの作り方の入り口を簡単に解説しました。<br />
小さなゲームならもう作れるのではないかと思います。</p>
<p>まだ少し不安が残る場合には、enchant.jsにサンプルとして同梱されているシューティングゲームのソースコードを読むことをおすすめします。<br />
今回の解説は何だったんだと怒られてしまいそうですが、シューティングゲームのコードは最初から各オブジェクトの定義が外だしされています。<br />
また、コメント内で詳しく実装の解説がなされているので、実際にゲームを作る際のenchant.jsの使い方がとても良く分かります。</p>
<p>まずは公式ドキュメントに慣れることが大事と思い、今回シューティングゲームではなくアクションゲームを題材に選んでみました。</p>
]]></content:encoded>
			<wfw:commentRss>http://freestyle.nvo.jp/archives/1020/feed</wfw:commentRss>
		</item>
		<item>
		<title>ゲームで使う数字の遊び方</title>
		<link>http://freestyle.nvo.jp/archives/1006</link>
		<comments>http://freestyle.nvo.jp/archives/1006#comments</comments>
		<pubDate>Mon, 19 Mar 2012 01:50:45 +0000</pubDate>
		<dc:creator>山平</dc:creator>
		
		<category><![CDATA[技術情報]]></category>

		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://freestyle.nvo.jp/?p=1006</guid>
		<description><![CDATA[ちょっと乱暴な言い方ですが、業務アプリと違ってゲームが保持する情報の多くがあまり本質的ではないものだったりします。
例えばアニメーション制御やエフェクトのためのオブジェクトがそれにあたります。
要はズバババ～ンって感じに [...]]]></description>
			<content:encoded><![CDATA[<p>ちょっと乱暴な言い方ですが、業務アプリと違ってゲームが保持する情報の多くがあまり本質的ではないものだったりします。<br />
例えばアニメーション制御やエフェクトのためのオブジェクトがそれにあたります。<br />
要はズバババ～ンって感じにキャラクターなどを表示するために色んな情報を扱う必要があり、結構面倒くさかったりします。</p>
<p>今回はそういった細々とした情報を扱う際の数字遊びを紹介します。<br />
enchant.jsでゲームを作ることを想定しているのでjavascriptで書きますが、基本的には言語を問いません。</p>
<p><span id="more-1006"></span></p>
<h3>三項演算子</h3>
<p>要はIF-ELSEの条件式と同じような動きですが、式の中にかけます。<br />
多用すると読みにくくなりますが、IF文や一次変数が増えた方が読みにくくなってしまうこともあります。<br />
ちょっとした条件をスマートに書きたいときに便利です。</p>
<p>次の例はキー入力を管理するオブジェクトの宣言部分です。<br />
イベント時などゲームは止めたくないけど入力はさせたくないときに空のオブジェクトを渡して操作できなくしています。</p>
<div class="igBar"><span id="ljavascript-20"><a href="#" onclick="javascript:showPlainTxt('javascript-20'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JavaScript:</span>
<div id="javascript-20">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> gi = <span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>._autoMode ? <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span> : game.<span style="color: #006600;">input</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p>
次の例はジャンプ中の落下加速度の計算部分です。<br />
落下スピードをマップの単位よりも大きくすると当たり判定が面倒になるので必要以上に加速しないようにしています。</p>
<div class="igBar"><span id="ljavascript-21"><a href="#" onclick="javascript:showPlainTxt('javascript-21'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JavaScript:</span>
<div id="javascript-21">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">speedY</span> += <span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">speedY</span> &lt;tileH + <span style="color: #CC0000;color:#800000;">1</span><span style="color: #66cc66;">&#41;</span> ? <span style="color: #CC0000;color:#800000;">1</span> : <span style="color: #CC0000;color:#800000;">0</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<h3>剰余(%演算子)</h3>
<p>ある数をある数で割ったときの余りを計算します。<br />
知っていれば普通、知らなければナルホドなのですが、アニメーションや当たり判定の時に重宝します。</p>
<p>次の例ではアニメーション画像３枚を切り替えるオフセット値を設定しています。<br />
加算、剰余、代入をまとめて書いてあるのでちょっと分かりにくい例ですね。</p>
<div class="igBar"><span id="ljavascript-22"><a href="#" onclick="javascript:showPlainTxt('javascript-22'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JavaScript:</span>
<div id="javascript-22">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> a=<span style="color: #CC0000;color:#800000;">0</span>; <span style="color: #009900; font-style: italic;">//=&gt; undefined</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">a = <span style="color: #66cc66;">&#40;</span>++a % <span style="color: #CC0000;color:#800000;">3</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">//=&gt; 1</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">a = <span style="color: #66cc66;">&#40;</span>++a % <span style="color: #CC0000;color:#800000;">3</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">//=&gt; 2</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">a = <span style="color: #66cc66;">&#40;</span>++a % <span style="color: #CC0000;color:#800000;">3</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">//=&gt; 0</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">a = <span style="color: #66cc66;">&#40;</span>++a % <span style="color: #CC0000;color:#800000;">3</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">//=&gt; 1</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">a = <span style="color: #66cc66;">&#40;</span>++a % <span style="color: #CC0000;color:#800000;">3</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">//=&gt; 2</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">a = <span style="color: #66cc66;">&#40;</span>++a % <span style="color: #CC0000;color:#800000;">3</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">//=&gt; 0 </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>次の例はキャラクタとマップの(Y方向の)当たり判定部分です。<br />
<a href="http://wise9.github.com/enchant.js/doc/ja/symbols/enchant.Map.html#hitTest">enchant.MapのhitTestメソッド</a>は、当たっているかどうかしか判定してくれません。<br />
なので、壁にめり込んだ部分を何ドット戻せば「接触している」状態になるかを自分で計算する必要があります。<br />
めり込んでいることが分かっている座標をマップの単位（タイルの大きさ）で割った余りがめり込んでいるドット数です。</p>
<div class="igBar"><span id="ljavascript-23"><a href="#" onclick="javascript:showPlainTxt('javascript-23'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JavaScript:</span>
<div id="javascript-23">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>map.<span style="color: #006600;">hitTest</span><span style="color: #66cc66;">&#40;</span>candiX, candiY<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #000066; font-weight: bold;">return</span> candiY % tileH; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<h3>切り捨て</h3>
<p>ある数をある数で割ったときに整数で結果が欲しいときがあります。<br />
言語によっては整数型は整数型でしか結果が返ってこないものもありますが、Javascriptでは明示的に切り捨てる必要があります。<br />
算数的にはごく普通なのですが、言語仕様としてはマイナス方向の切り捨てには注意が必要な部分です。</p>
<p>Math.floorの場合、「もっとも小さい整数値」を返します。<br />
マイナスの時、整数部の値が変わることに注意が必要です。</p>
<div class="igBar"><span id="ljavascript-24"><a href="#" onclick="javascript:showPlainTxt('javascript-24'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JavaScript:</span>
<div id="javascript-24">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Math.<span style="color: #006600;">floor</span><span style="color: #66cc66;">&#40;</span><span style="color: #CC0000;color:#800000;">30</span>.<span style="color: #CC0000;color:#800000;">12345</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">//=&gt; 30</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Math.<span style="color: #006600;">floor</span><span style="color: #66cc66;">&#40;</span>-<span style="color: #CC0000;color:#800000;">30</span>.<span style="color: #CC0000;color:#800000;">12345</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">//=&gt; -31 ←正の数と結果が違う！</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Math.<span style="color: #006600;">floor</span><span style="color: #66cc66;">&#40;</span><span style="color: #CC0000;color:#800000;">30</span>.<span style="color: #CC0000;color:#800000;">54321</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">//=&gt; 30</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Math.<span style="color: #006600;">floor</span><span style="color: #66cc66;">&#40;</span>-<span style="color: #CC0000;color:#800000;">30</span>.<span style="color: #CC0000;color:#800000;">54321</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">//=&gt; -31 ←正の数と結果が違う！ </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Math.roundの場合、「四捨五入した整数値」を返します。<br />
少数部が0.5以上の時、整数部の値が変わることに注意が必要です。</p>
<div class="igBar"><span id="ljavascript-25"><a href="#" onclick="javascript:showPlainTxt('javascript-25'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JavaScript:</span>
<div id="javascript-25">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Math.<span style="color: #006600;">round</span><span style="color: #66cc66;">&#40;</span><span style="color: #CC0000;color:#800000;">30</span>.<span style="color: #CC0000;color:#800000;">12345</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">//=&gt; 30</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Math.<span style="color: #006600;">round</span><span style="color: #66cc66;">&#40;</span>-<span style="color: #CC0000;color:#800000;">30</span>.<span style="color: #CC0000;color:#800000;">12345</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">//=&gt; -30</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Math.<span style="color: #006600;">round</span><span style="color: #66cc66;">&#40;</span><span style="color: #CC0000;color:#800000;">30</span>.<span style="color: #CC0000;color:#800000;">54321</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">//=&gt; 31 ←小数点以下の値で結果が変わる！</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Math.<span style="color: #006600;">round</span><span style="color: #66cc66;">&#40;</span>-<span style="color: #CC0000;color:#800000;">30</span>.<span style="color: #CC0000;color:#800000;">54321</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">//=&gt; -31 ←小数点以下の値で結果が変わる！ </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<h3>【参考】剰余と切り捨てで2次元を表現する</h3>
<p>剰余と切り捨てを使うことで、平面座標を表現することができます。<br />
<a href="http://wise9.github.com/enchant.js/doc/ja/symbols/enchant.Sprite.html">enchant.Sprite</a>の画像位置計算でも使われていると思います。</p>
<div class="igBar"><span id="ljavascript-26"><a href="#" onclick="javascript:showPlainTxt('javascript-26'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JavaScript:</span>
<div id="javascript-26">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000066; font-weight: bold;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i=<span style="color: #CC0000;color:#800000;">0</span>; i&lt;<span style="color: #66cc66;">&#40;</span><span style="color: #CC0000;color:#800000;">3</span>*<span style="color: #CC0000;color:#800000;">3</span><span style="color: #66cc66;">&#41;</span>; i++<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">console.<span style="color: #006600;">log</span><span style="color: #66cc66;">&#40;</span> <span style="color: #3366CC;">"x:"</span> + <span style="color: #66cc66;">&#40;</span>i%<span style="color: #CC0000;color:#800000;">3</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #3366CC;">", y:"</span> + <span style="color: #66cc66;">&#40;</span>Math.<span style="color: #006600;">floor</span><span style="color: #66cc66;">&#40;</span>i/<span style="color: #CC0000;color:#800000;">3</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">//=&gt; x:0, y:0</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">//=&gt; x:1, y:0</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">//=&gt; x:2, y:0</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">//=&gt; x:0, y:1</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">//=&gt; x:1, y:1</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">//=&gt; x:2, y:1</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">//=&gt; x:0, y:2</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">//=&gt; x:1, y:2</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">//=&gt; x:2, y:2 </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<h3>カウンタ各種</h3>
<p>最後に<br />
特にゲームではちょっとした場合にちょっとした値を使いたいことがよくあります。<br />
これらすべてに宣言、代入、条件分岐を書いているとコードが膨らんでしまって肝心な部分が読みにくくなります。<br />
簡潔にかけそうな式を紹介します。</p>
<p>0～Xを繰り返す<br />
上の剰余で説明しているので省略します。</p>
<p>1と-1を繰り返す</p>
<div class="igBar"><span id="ljavascript-27"><a href="#" onclick="javascript:showPlainTxt('javascript-27'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JavaScript:</span>
<div id="javascript-27">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> a=<span style="color: #CC0000;color:#800000;">1</span>; <span style="color: #009900; font-style: italic;">//=&gt; undefined</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">a *= -<span style="color: #CC0000;color:#800000;">1</span>; <span style="color: #009900; font-style: italic;">//=&gt; -1</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">a *= -<span style="color: #CC0000;color:#800000;">1</span>; <span style="color: #009900; font-style: italic;">//=&gt; 1</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">a *= -<span style="color: #CC0000;color:#800000;">1</span>; <span style="color: #009900; font-style: italic;">//=&gt; -1 </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>0とXを繰り返す(例は0と1の繰り返し)</p>
<div class="igBar"><span id="ljavascript-28"><a href="#" onclick="javascript:showPlainTxt('javascript-28'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JavaScript:</span>
<div id="javascript-28">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> a=<span style="color: #CC0000;color:#800000;">1</span>; <span style="color: #009900; font-style: italic;">//=&gt; undefined</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">a=<span style="color: #CC0000;color:#800000;">1</span>-a; <span style="color: #009900; font-style: italic;">//=&gt; 0</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">a=<span style="color: #CC0000;color:#800000;">1</span>-a; <span style="color: #009900; font-style: italic;">//=&gt; 1</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">a=<span style="color: #CC0000;color:#800000;">1</span>-a; <span style="color: #009900; font-style: italic;">//=&gt; 0</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">a=<span style="color: #CC0000;color:#800000;">1</span>-a; <span style="color: #009900; font-style: italic;">//=&gt; 1 </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://freestyle.nvo.jp/archives/1006/feed</wfw:commentRss>
		</item>
		<item>
		<title>GIMPでドット絵を加工する</title>
		<link>http://freestyle.nvo.jp/archives/986</link>
		<comments>http://freestyle.nvo.jp/archives/986#comments</comments>
		<pubDate>Fri, 24 Feb 2012 10:50:42 +0000</pubDate>
		<dc:creator>山平</dc:creator>
		
		<category><![CDATA[技術情報]]></category>

		<category><![CDATA[オープンソース]]></category>

		<category><![CDATA[ツール]]></category>

		<guid isPermaLink="false">http://freestyle.nvo.jp/?p=986</guid>
		<description><![CDATA[ゲーム作りに欠かせないのが画像です。
自分で作るのは大変な労力がかかりますが、世の中には高品質な素材がたくさん提供されています。
ライセンスに注意して有効に活用したいものです。
ほとんどの素材はそれぞれの用途を想定した形 [...]]]></description>
			<content:encoded><![CDATA[<p>ゲーム作りに欠かせないのが画像です。<br />
自分で作るのは大変な労力がかかりますが、世の中には高品質な素材がたくさん提供されています。<br />
ライセンスに注意して有効に活用したいものです。</p>
<p>ほとんどの素材はそれぞれの用途を想定した形で配布されていますが、必ずしも自分の思った通りに配布されているものでもありません。<br />
今回は<a href="http://www.gimp.org/">GIMP</a>を使ってそんな素材のうちドット絵を自分の用途に合わせて加工する方法を紹介します。</p>
<p>※使用と加工の際には配布元のライセンスにご注意ください！<br />
※サンプル画像として<a href="http://enchantjs.com/">enchant.js</a>に同梱されているものを利用しています。</p>
<p><span id="more-986"></span></p>
<h3>グリッドを表示する</h3>
<p>ドット絵は同じサイズの画像を複数枚入れ替えてアニメーションを実現します。<br />
加工の際には画像の配置がずれてしまわないように注意が必要です。<br />
その労力を軽減するために、画像サイズに合わせてグリッドを表示しましょう。</p>
<p>メニューから「表示 &gt; グリッドを表示」でグリッドが表示できますが、そのままでは画像のサイズと合っていないことがあります。</p>
<p><a href="http://freestyle.nvo.jp/wp-content/uploads/2012/02/0_disp_grid.png"><img class="aligncenter size-medium wp-image-987" title="0_disp_grid" src="http://freestyle.nvo.jp/wp-content/uploads/2012/02/0_disp_grid-300x278.png" alt="0_disp_grid" width="300" height="278" /></a></p>
<p><a href="http://freestyle.nvo.jp/wp-content/uploads/2012/02/1_gapped_grid.png"><img class="aligncenter size-medium wp-image-988" title="1_gapped_grid" src="http://freestyle.nvo.jp/wp-content/uploads/2012/02/1_gapped_grid-266x300.png" alt="1_gapped_grid" width="266" height="300" /></a></p>
<p>メニューから「画像 &gt; グリッドの設定」でグリッドのサイズなどを変更することができます。</p>
<p><a href="../wp-content/uploads/2012/02/2_menu_grid.png"><img class="aligncenter size-medium wp-image-989" title="2_menu_grid" src="../wp-content/uploads/2012/02/2_menu_grid-300x221.png" alt="2_menu_grid" width="300" height="221" /></a></p>
<p><a href="http://freestyle.nvo.jp/wp-content/uploads/2012/02/3_grid_setting.png"><img class="aligncenter size-medium wp-image-990" title="3_grid_setting" src="http://freestyle.nvo.jp/wp-content/uploads/2012/02/3_grid_setting-280x300.png" alt="3_grid_setting" width="280" height="300" /></a></p>
<p><a href="http://freestyle.nvo.jp/wp-content/uploads/2012/02/4_matched_grid.png"><img class="aligncenter size-medium wp-image-991" title="4_matched_grid" src="http://freestyle.nvo.jp/wp-content/uploads/2012/02/4_matched_grid-300x221.png" alt="4_matched_grid" width="300" height="221" /></a></p>
<p>これでグリッドが画像にぴったり合いました。</p>
<h3>画像の切り抜き</h3>
<p>配置を変えたり、1枚の画像を複数枚に分割したりしたい場合には画像の切り抜きを行います。</p>
<p>ツールボックスから切り抜きツールを選択して、切り取りたい部分をドラッグすれば範囲が選択できます。</p>
<p><a href="http://freestyle.nvo.jp/wp-content/uploads/2012/02/5_menu_cut_image.png"><img class="aligncenter size-medium wp-image-992" title="5_menu_cut_image" src="http://freestyle.nvo.jp/wp-content/uploads/2012/02/5_menu_cut_image-300x177.png" alt="5_menu_cut_image" width="300" height="177" /></a></p>
<p>選択した範囲の上下左右はマウスで微調整できます。</p>
<p><a href="http://freestyle.nvo.jp/wp-content/uploads/2012/02/6_image_cutting.png"><img class="aligncenter size-medium wp-image-993" title="6_image_cutting" src="http://freestyle.nvo.jp/wp-content/uploads/2012/02/6_image_cutting-300x234.png" alt="6_image_cutting" width="300" height="234" /></a></p>
<p>Enterキーで切り抜きを実行します。</p>
<p><a href="http://freestyle.nvo.jp/wp-content/uploads/2012/02/6_image_cutting.png"></a><a href="http://freestyle.nvo.jp/wp-content/uploads/2012/02/7_cutted_image.png"><img class="aligncenter size-medium wp-image-994" title="7_cutted_image" src="http://freestyle.nvo.jp/wp-content/uploads/2012/02/7_cutted_image-300x130.png" alt="7_cutted_image" width="300" height="130" /></a></p>
<p>ちなみに、範囲選択ツールでも同様に選択できますので、切り抜きではなく、コピーを行いたい場合はこちらを使います。</p>
<p><a href="http://freestyle.nvo.jp/wp-content/uploads/2012/02/7_2_select_tool.png"><img class="aligncenter size-medium wp-image-998" title="7_2_select_tool" src="http://freestyle.nvo.jp/wp-content/uploads/2012/02/7_2_select_tool-267x300.png" alt="7_2_select_tool" width="267" height="300" /></a></p>
<h3>背景色の透明化</h3>
<p>配布されている素材の背景が透過されていないものも少なくありません。<br />
これを綺麗に取り除くには以下のようにします。</p>
<p>ツールボックスからファジー選択ツールを選択して、切り取りたい背景色をクリックすると、同じ色がすべて選択されます。</p>
<p><a href="http://freestyle.nvo.jp/wp-content/uploads/2012/02/8_remove_back.png"><img class="aligncenter size-medium wp-image-995" title="8_remove_back" src="http://freestyle.nvo.jp/wp-content/uploads/2012/02/8_remove_back-300x176.png" alt="8_remove_back" width="300" height="176" /></a></p>
<p><a href="http://freestyle.nvo.jp/wp-content/uploads/2012/02/9_select_to_remove.png"><img class="aligncenter size-medium wp-image-996" title="9_select_to_remove" src="http://freestyle.nvo.jp/wp-content/uploads/2012/02/9_select_to_remove-300x258.png" alt="9_select_to_remove" width="300" height="258" /></a></p>
<p>背景色が選択されている状態でDeleteキーを押せば取り除けます。</p>
<p><a href="http://freestyle.nvo.jp/wp-content/uploads/2012/02/a_removed_back.png"><img class="aligncenter size-medium wp-image-997" title="a_removed_back" src="http://freestyle.nvo.jp/wp-content/uploads/2012/02/a_removed_back-300x258.png" alt="a_removed_back" width="300" height="258" /></a></p>
<p>なお、連続していない背景色部分はこの操作だけでは取り除けません。<br />
これは仕方ないので丁寧に消していきます。</p>
<p><a href="http://freestyle.nvo.jp/wp-content/uploads/2012/02/b_need_remove_back.png"><img class="aligncenter size-medium wp-image-999" title="b_need_remove_back" src="http://freestyle.nvo.jp/wp-content/uploads/2012/02/b_need_remove_back-300x281.png" alt="b_need_remove_back" width="300" height="281" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://freestyle.nvo.jp/archives/986/feed</wfw:commentRss>
		</item>
		<item>
		<title>enchant.jsの概説</title>
		<link>http://freestyle.nvo.jp/archives/982</link>
		<comments>http://freestyle.nvo.jp/archives/982#comments</comments>
		<pubDate>Sun, 12 Feb 2012 06:29:57 +0000</pubDate>
		<dc:creator>山平</dc:creator>
		
		<category><![CDATA[技術情報]]></category>

		<category><![CDATA[HTML5]]></category>

		<category><![CDATA[JavaScript]]></category>

		<category><![CDATA[開発環境]]></category>

		<guid isPermaLink="false">http://freestyle.nvo.jp/?p=982</guid>
		<description><![CDATA[今回はJavascript+HTML5で簡単にゲームが作れるenchant.jsを紹介します。
少し触ってみましたが、シンプルさとJavascriptの柔軟性、妥当なフレームワーク構成でちょっとしたゲームならすぐに作れま [...]]]></description>
			<content:encoded><![CDATA[<p>今回はJavascript+HTML5で簡単にゲームが作れる<a href="http://enchantjs.com/">enchant.js</a>を紹介します。</p>
<p>少し触ってみましたが、シンプルさとJavascriptの柔軟性、妥当なフレームワーク構成でちょっとしたゲームならすぐに作れます。<br />
凝ったゲームを作る場合のクラス拡張がとても簡単で、設計にセンスを感じます。</p>
<p><span id="more-982"></span></p>
<p>すでに発表から時間が経っていること、日本初のフレームワークであることから、ネット上に情報はたくさんあります。<br />
ですが、本家の情報がとてもわかりやすいので、本家の情報とサンプルコードで学ぶのがよいと思います。</p>
<p><a href="http://enchantjs.com/ja/reference.html">Reference enchant.js</a></p>
<p>enchant.jsを構成する、ゲーム作りに必要なクラスについて、ひと通りざっくりと説明しています。<br />
サンプルコードを読み進める際にはこのぐらいの情報がちょうどよいと思います。</p>
<p><a href="http://wise9.github.com/enchant.js/doc/ja/">公式ドキュメント（JSDoc）</a></p>
<p>ソースコードから抽出したドキュメントと思われるリファレンスです。<br />
説明は少なめですが機能が網羅されているので、詳しく調べたいときに重宝します。<br />
enchant.jsにも付属していますので、通常はそちら（付属版）をみることになります。</p>
<p><a href="http://9leap.net/">9leap</a></p>
<p>enchant.jsの投稿サイトです。<br />
主に若い人向けにゲームコンテストを開催しているようですが、参考になるゲームやハマってしまうゲーム、便利なプラグインが発表されていたりと見ていて飽きません。</p>
<p>以上、enchant.jsに関する情報を紹介しました。<br />
次回以降で実際にゲームを作る作業を紹介したいと思います。</p>
]]></content:encoded>
			<wfw:commentRss>http://freestyle.nvo.jp/archives/982/feed</wfw:commentRss>
		</item>
		<item>
		<title>リファクタリングという技術</title>
		<link>http://freestyle.nvo.jp/archives/961</link>
		<comments>http://freestyle.nvo.jp/archives/961#comments</comments>
		<pubDate>Fri, 03 Feb 2012 12:46:09 +0000</pubDate>
		<dc:creator>yasukuni</dc:creator>
		
		<category><![CDATA[技術情報]]></category>

		<guid isPermaLink="false">http://freestyle.nvo.jp/?p=961</guid>
		<description><![CDATA[コーディングを行なっている際に次のような経験はないでしょうか？
・ちょっと前にも同じようなことを記述した記憶がある。
・似たような機能(画面)が既に存在したので、コピー＆ペーストでコードを複製した。
・コードの中にコメン [...]]]></description>
			<content:encoded><![CDATA[<p>コーディングを行なっている際に次のような経験はないでしょうか？</p>
<blockquote><p>・ちょっと前にも同じようなことを記述した記憶がある。<br />
・似たような機能(画面)が既に存在したので、コピー＆ペーストでコードを複製した。<br />
・コードの中にコメントを記載しないと意味が解らない変数やマジックナンバーが沢山ある。<br />
・バグが入り込んだ際に、原因の特定に時間を要する(修正が困難)又は、途中でトレースするのが嫌になった。<br />
・仕様変更による影響が多岐に及び、対応/評価に莫大な時間を要した。</p></blockquote>
<p>この様な経験があれば、リファクタリングが必要なのかもしれません。</p>
<p>リファクタリングとは機能を変更することなく、コードの組み換え等を行い仕様変更への耐性、保守性の向上を行う技術です。<br />
この技術の根本には、仕様というのは変動的であり、多くの場合に漏れ(抜け)があるという考えのもと行われます。<br />
例えば、同様のコードが見つかった際に、共通化する小さな努力を怠ったり複製を行うと、修正が発生した場合に対象が多岐に及ぶことは避けられません。</p>
<p><span id="more-961"></span>ただ、このリファクタリングを行う際に、一番注意しなければならないことは<br />
『リファクタリング前後で動作が変わらないこと』です。<br />
ただ闇雲に行なってしまうと、正常に動作していた機能が動作しなくなるばかりか、芋づる式に修正範囲が広がってしまい、逆に手が付けれないことにもなりかねません。<br />
大きなリファクタリングを行う際は、テスト方法も併せて十分検討する必要があります。</p>
<p>次は小さなリファクタリングのサンプルです。この処理は何を意味するのでしょうか？</p>
<blockquote><p>int work = 100 / 37.5782;</p></blockquote>
<p>マジックナンバー"37.5782"がよくわからないので隠蔽するために、処理をメソッド化しました。(※37.5782は100m世界記録の時速)</p>
<blockquote><p>int hour = this.getNecessaryHourForRunning(100);</p>
<p>function int getEstimateHour(int km){<br />
return km / 37.5782;<br />
}</p></blockquote>
<p>行数は増えてしまいましたが、処理を共通化することだけがリファクタリングの目的ではありません。<br />
このメソッドを見れば、走るために必要な時間(h)が取得できることが一目でわかります。<br />
もし仮に、ヒトが走ると疲れることを考慮に入れる仕様が追加された場合も、このメソッドを修正すれば非常に小さなスコープで影響が収まります。</p>
<p>まず、この様な小さなことから始めてみると良いのかもしれません。</p>
]]></content:encoded>
			<wfw:commentRss>http://freestyle.nvo.jp/archives/961/feed</wfw:commentRss>
		</item>
		<item>
		<title>RoRライクのJavaフレームワーク「Play Framework」</title>
		<link>http://freestyle.nvo.jp/archives/947</link>
		<comments>http://freestyle.nvo.jp/archives/947#comments</comments>
		<pubDate>Tue, 24 Jan 2012 03:51:04 +0000</pubDate>
		<dc:creator>nakas</dc:creator>
		
		<category><![CDATA[技術情報]]></category>

		<category><![CDATA[Java]]></category>

		<category><![CDATA[オープンソース]]></category>

		<guid isPermaLink="false">http://freestyle.nvo.jp/?p=947</guid>
		<description><![CDATA[普段はJavaをメインに使っていながら、最近はHTML5/JS/Android/iOSなどクライアントサイドの技術が賑わっていることもあって、サーバーサイドの技術、特にWebフレームワークには目を向けていなかったんですが [...]]]></description>
			<content:encoded><![CDATA[<p>普段はJavaをメインに使っていながら、最近はHTML5/JS/Android/iOSなどクライアントサイドの技術が賑わっていることもあって、サーバーサイドの技術、特にWebフレームワークには目を向けていなかったんですが、Java/Scalaエンジニア界隈では、 "Play Framework" が最近注目されているらしいです。</p>
<p>http://www.playframework.org/</p>
<p>JavaによるWebアプリケーション開発を容易にすることを目的とした、<br />
Ruby on RailsライクのOSSフレームワークです。</p>
<p>概要や特徴などは公式サイトで分かりやすく説明されているので、そちらを参照してください。今回紹介する環境構築手順や開発フローが見られる10分程度のムービーもあります。<br />
公式ではありませんが、翻訳サイトもあります。</p>
<p>http://playdocja.appspot.com/</p>
<p>早速、ローカル環境(WindowsXP)に開発環境を構築してみます。<br />
なお、Java5以降がインストールされている前提です。</p>
<p>1.最新バージョン(v1.2.4)をダウンロードします。</p>
<p>http://www.playframework.org/download</p>
<p>ファイル：play-1.2.4.zip</p>
<p>2.ダウンロードしたファイルを任意のフォルダに展開します。</p>
<p>今回は以下のディレクトリに展開<br />
D:\work\play-1.2.4</p>
<p>3.コマンドプロンプトを起動して、新規アプリケーション作成用のコマンドを実行します。</p>
<p># cd D:\work\play-1.2.4<br />
# play new myapp</p>
<p>途中でアプリケーション名を聞かれるので適当に入力します。</p>
<p>4.アプリケーションの起動コマンドを実行します。</p>
<p># play run myapp</p>
<p>5.ブラウザを開いて、URLを入力します。</p>
<p>http://localhost:9000/</p>
<p>基本的な環境構築は以上です。</p>
<p>Tomcatなどのアプリケーションサーバ上で動作させることもできますが、スタンドアロンで動作するので、すぐにアプリケーションの開発に取り掛かれます。<br />
テキストエディタをはじめ、Eclipseなどお好みのIDEで開発できるのもGoodです。</p>
<p>環境構築だけでなく、アプリケーションの開発においても、効率的に開発できる機能がたくさん含まれているので、順次試していきたいと思います。</p>
]]></content:encoded>
			<wfw:commentRss>http://freestyle.nvo.jp/archives/947/feed</wfw:commentRss>
		</item>
		<item>
		<title>Rubydoctestの挙動メモ</title>
		<link>http://freestyle.nvo.jp/archives/938</link>
		<comments>http://freestyle.nvo.jp/archives/938#comments</comments>
		<pubDate>Fri, 06 Jan 2012 09:08:44 +0000</pubDate>
		<dc:creator>山平</dc:creator>
		
		<category><![CDATA[技術情報]]></category>

		<category><![CDATA[Ruby]]></category>

		<category><![CDATA[ツール]]></category>

		<guid isPermaLink="false">http://freestyle.nvo.jp/?p=938</guid>
		<description><![CDATA[前回、Rubydoctestのドキュメントを邦訳しました（rubydoctest - Example Usage の邦訳）。
本家のドキュメントも少ないのですが、他に情報らしい情報もほとんど見当たりません。
唯一見つけた [...]]]></description>
			<content:encoded><![CDATA[<p>前回、Rubydoctestのドキュメントを邦訳しました<a href="../archives/931">（rubydoctest - Example Usage の邦訳）</a>。<br />
<a href="https://github.com/tablatom/rubydoctest/wiki/">本家のドキュメント</a>も少ないのですが、他に情報らしい情報もほとんど見当たりません。<br />
唯一見つけた情報も使い方というよりは紹介ですし、かなり古い記事です。</p>
<ul>
<li><a href="http://www.infoq.com/jp/news/2008/07/ruby-doctest">DocTest 1.0 For Ruby リリース</a></li>
</ul>
<p>なので使ってみた結果をここに記録しておきます。</p>
<p><span id="more-938"></span></p>
<h3>呼び出しに関すること</h3>
<h4>微妙なパス指定</h4>
<p>ファイル名の指定に"*(任意の文字列)","?(任意の文字)"が使えるようですが、"**(任意の階層)"は使えないようです。<br />
なので、</p>
<ul>
<li>./aaa/astk.rb</li>
<li>./aaa/bbb/msptmy.rb</li>
<li>./xxx/inds.rb</li>
</ul>
<p>上のような構成で"rubydoctest **/*.rb"とタイプしてもmsptmy.rbはテストされません。</p>
<blockquote><p>rubydoctest **/*.rb<br />
=== Testing 'xxx/hoge.rb'...<br />
OK  | Default Test<br />
1 comparisons, 1 doctests, 0 failures, 0 errors<br />
=== Testing 'aaa/astk.rb'...<br />
OK  | Default Test<br />
1 comparisons, 1 doctests, 0 failures, 0 errors</p></blockquote>
<p>ここはトップレベルの指定だけで全部テストしてほしいところです。</p>
<h4>シンプルなirbセッションを貼り付ける</h4>
<p>irbの結果を張り付けるだけでテストコードが作成できるのは便利なのですが、シンプルなirbセッションを張り付けないと怒られてしまいます。<br />
「シンプルなirbセッション」とは、「--simple-prompt」オプションを指定して起動したirbセッションのプロンプトのことを意味しています。</p>
<p>シンプルでない（デフォルト）の場合</p>
<blockquote><p>$ irb<br />
irb(main):001:0&gt; 1+1<br />
=&gt; 2<br />
irb(main):002:0&gt;</p></blockquote>
<p>シンプルなirbセッションの場合</p>
<blockquote><p>$ /usr/bin/irb --simple-prompt<br />
&gt;&gt; 1*1<br />
=&gt; 1<br />
&gt;&gt;</p></blockquote>
<p>なので「.bashrc」にエイリアスをつけておくのがいいと思います。</p>
<blockquote><p>alias irb='irb --simple-prompt'</p></blockquote>
<h3>テストに関すること</h3>
<p>まず、挙動確認に使ったソースと動作結果を示します。</p>
<p>ソース「doctest.rb」</p>
<div class="igBar"><span id="lruby-30"><a href="#" onclick="javascript:showPlainTxt('ruby-30'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-30">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000; font-style:italic;">#!/usr/bin/ruby</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000; font-style:italic;">#doctest: 事前にインスタンスが作れる</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000; font-style:italic;"># テスト中は変数が生きているので、以降のテストでも利用可能</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000; font-style:italic;"># クラスの場合などに便利</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000; font-style:italic;">#&gt;&gt; d = Doctesttest.new</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000; font-style:italic;">#&gt;&gt; d.class</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000; font-style:italic;">#=&gt; Doctesttest</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">class</span> Doctesttest</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000; font-style:italic;">#doctest: 足し算</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000; font-style:italic;">#&gt;&gt; d.add(1,2)</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000; font-style:italic;">#=&gt; 3</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">def</span> add<span style="color:#006600; font-weight:bold;">&#40;</span>a, b<span style="color:#006600; font-weight:bold;">&#41;</span>; a + b; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000; font-style:italic;">#doctestディレクティブがないと前のテストに</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000; font-style:italic;">#含まれている用に見えるけど、無視はされてないっぽい</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000; font-style:italic;">#&gt;&gt; d.sub(1,2)</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000; font-style:italic;">#=&gt; -1</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#CC0066; font-weight:bold;">sub</span><span style="color:#006600; font-weight:bold;">&#40;</span>a, b<span style="color:#006600; font-weight:bold;">&#41;</span>; a - b; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000; font-style:italic;">#doctest: ここでdoメソッドを試す</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000; font-style:italic;"># !!!ディレクティブで止める</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000; font-style:italic;">#!!!</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#9966CC; font-weight:bold;">do</span><span style="color:#006600; font-weight:bold;">&#40;</span>expression<span style="color:#006600; font-weight:bold;">&#41;</span>; <span style="color:#CC0066; font-weight:bold;">eval</span> expression; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000; font-style:italic;">#doctest: !!!ディレクティブはexitで抜ける</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000; font-style:italic;"># 抜けた後はrubydoctestのセッションに戻る</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000; font-style:italic;"># 2回&quot;exit&quot;とタイプしないといけないのはなぜ？</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000; font-style:italic;">#&gt;&gt; d.do &quot;1+1&quot;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000; font-style:italic;">#=&gt; 2</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>テスト結果</p>
<blockquote><p>$ rubydoctest doctest.rb<br />
=== Testing 'doctest.rb'...<br />
OK  | テスト中の変数は最後まで生き残る<br />
なのでクラスのインスタンスを最初に作れば<br />
以降のメソッドで使いまわせる<br />
OK  | 足し算<br />
OK  | ここでdoメソッドを試す<br />
!!!ディレクティブで止める<br />
&gt;&gt; d.do "1+2+3"<br />
=&gt; 6<br />
&gt;&gt; exit<br />
&gt;&gt; exit<br />
OK  | !!!ディレクティブはexitで抜ける<br />
抜けた後はrubydoctestのセッションに戻る<br />
2回"exit"とタイプしないといけないのはなぜ？<br />
4 comparisons, 6 doctests, 0 failures, 0 errors<br />
$</p></blockquote>
<h4>事前に宣言したオブジェクトを使える</h4>
<p>ソースの先頭でDoctesttestクラスのインスタンスを作成しています。<br />
テスト中はこのインスタンスが生きているので使い回すことができます。<br />
クラスのメソッドを確認するのに毎回オブジェクトを作る必要はありません。</p>
<h4>doctestディレクティブ</h4>
<p>doctestディレクティブが記述されていないテストは「OK | メッセージ」という形式で結果が出力されませんが、テストとしてはカウントされているようです。<br />
（最後の「4 comparisons, 6 doctests」に注目）</p>
<p>クラスのテストの場合には、1メソッドに1doctestディレクティブで記述するのが読みやすそうです。</p>
<h4>!!!ディレクティブ</h4>
<p>テストの途中でirbセッションを触ることができます。<br />
大量の自動テストの途中で止まってしまうことを考えると不便ですが、デバッグ中に毎回入力する内容までを自動化すると便利です。<br />
ただし、エラーが発生した場合、通常のirbセッションと違って変なスタックトレースが出力されてしまって問題のある箇所が特定できません。。。</p>
<p>テストにセッションを返す際は「exit」と入力すればよいとドキュメントにあるのですが、なぜか2回入力しないと戻りませんでした。</p>
<h3>結論</h3>
<p>便利そうで意外と不便なのですが、それでも使いたくなる魅力を感じるのは私だけでしょうか？<br />
何を重視するかによっても評価は変わってくると思いますが、</p>
<ul>
<li>ソースファイルの中に一緒に記述できるので、忘れたころにソースを触る必要が合った場合にテストコード紛失の恐れがない</li>
<li>コメントとして記述するので<a href="http://ruby.gfd-dennou.org/tutorial/rdoc/">rdoc</a>等のドキュメントにもテストコードが出力され、一粒で二度おいしい（これもソースファイルだけの管理で紛失の心配がない）</li>
</ul>
<p>この二点にたまらない魅力を感じるのは、私の「作っては放置し、久しぶりに触っては苦労する」という習慣のせいなのでしょうが。。。</p>
<p>以上です。</p>
]]></content:encoded>
			<wfw:commentRss>http://freestyle.nvo.jp/archives/938/feed</wfw:commentRss>
		</item>
		<item>
		<title>rubydoctest - Example Usage の邦訳</title>
		<link>http://freestyle.nvo.jp/archives/931</link>
		<comments>http://freestyle.nvo.jp/archives/931#comments</comments>
		<pubDate>Mon, 05 Dec 2011 10:36:18 +0000</pubDate>
		<dc:creator>山平</dc:creator>
		
		<category><![CDATA[技術情報]]></category>

		<category><![CDATA[Ruby]]></category>

		<category><![CDATA[翻訳]]></category>

		<guid isPermaLink="false">http://freestyle.nvo.jp/?p=931</guid>
		<description><![CDATA[前回（レーベンシュタイン距離で文字列の類似度を測る）引用させていただいたサイトさま（Moderation is a fatal thing. Nothing succeeds like excess.）でとても気になる記 [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://freestyle.nvo.jp/archives/925">前回（レーベンシュタイン距離で文字列の類似度を測る）</a>引用させていただいた<a href="http://d.hatena.ne.jp/kenkitii/20090204/ruby_levenshtein_distance">サイトさま（Moderation is a fatal thing. Nothing succeeds like excess.）</a>でとても気になる記述がありました。</p>
<blockquote><p>で、、、この手のコードを書いたときに、ちょっとしたテストも書いときたいなあ、と思うんですよね。Python だと、doctest っていうコメントにテストを埋め込める便利なのがあるんですが、ruby にもないものかと思って探したらありました。rubydoctestってやつが。</p></blockquote>
<p><a href="http://rubydoctest.rubyforge.org/">Ruby DocTest</a></p>
<p>気になりすぎて調査したかったので前回はあえて触れず、今回紹介されている<a href="https://github.com/tablatom/rubydoctest/wiki/example-usage">rubydoctestの使用例のページ（Example Usage）</a>を訳してみました。<br />
ほとんどコードでかつコード中の文は訳していないのですが、雰囲気はつかめると思います。</p>
<p><span id="more-931"></span></p>
<h3>Example Usage<br />
使用例</h3>
<blockquote><p>Create a .doctest document, for example, “simple.doctest”, and begin documenting your application, like this:</p></blockquote>
<p>例えば"simple.doctest"のような、拡張子"doctest"のドキュメントを生成します。<br />
そして以下のようにあなたのアプリケーションについて記述します。</p>
<div class="igBar"><span id="lruby-39"><a href="#" onclick="javascript:showPlainTxt('ruby-39'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-39">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Here is an example doctest file <span style="color:#006600; font-weight:bold;">&#40;</span>say called simple.<span style="color:#9900CC;">doctest</span><span style="color:#006600; font-weight:bold;">&#41;</span>:</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">This is an example <span style="color:#CC0066; font-weight:bold;">test</span> that succeeds</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&gt;&gt; <span style="color:#006666;color:#800000;">1</span> + <span style="color:#006666;color:#800000;">2</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">=&gt; <span style="color:#006666;color:#800000;">3</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">And</span> here’s a <span style="color:#CC0066; font-weight:bold;">test</span> that will <span style="color:#CC0066; font-weight:bold;">fail</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&gt;&gt; <span style="color:#006666;color:#800000;">1</span> + <span style="color:#006666;color:#800000;">2</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">=&gt; <span style="color:#006666;color:#800000;">4</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#CC0066; font-weight:bold;">Test</span> a some multiline statements</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&gt;&gt; <span style="color:#9966CC; font-weight:bold;">class</span> Person</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">attr_accessor :name</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&gt;&gt; Person</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">=&gt; Person</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&gt;&gt; <span style="color:#CC0066; font-weight:bold;">p</span> = Person.<span style="color:#9900CC;">new</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&gt;&gt; <span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">name</span> = “Tom”</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&gt;&gt; <span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">name</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">=&gt; “Tom”</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">doctest: you <span style="color:#CC0066; font-weight:bold;">split</span> a file into separate named tests by adding a doctest: directive</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&gt;&gt; <span style="color:#006666;color:#800000;">1</span> + <span style="color:#006666;color:#800000;">2</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">=&gt; <span style="color:#006666;color:#800000;">4</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<blockquote><p>Or, put your pastes in as comments within your ruby code, say, factorial.rb, like this:</p></blockquote>
<p>もしくは以下のようにあなたのアプリケーション"factorial.rb"のコードにコメントとして記述することもできます。</p>
<div class="igBar"><span id="lruby-40"><a href="#" onclick="javascript:showPlainTxt('ruby-40'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-40">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">doctest: factorial should give correct results <span style="color:#9966CC; font-weight:bold;">for</span> <span style="color:#006666;color:#800000;">0</span> to <span style="color:#006666;color:#800000;">2</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&gt;&gt; factorial<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;color:#800000;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">=&gt; <span style="color:#006666;color:#800000;">1</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&gt;&gt; factorial<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">=&gt; <span style="color:#006666;color:#800000;">1</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000; font-style:italic;">#</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">comments describing the parameters can be included</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">this is how it works with <span style="color:#006666;color:#800000;">2</span>:</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&gt;&gt; factorial<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;color:#800000;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">=&gt; <span style="color:#006666;color:#800000;">2</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">def</span> factorial<span style="color:#006600; font-weight:bold;">&#40;</span>n<span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">if</span> n == <span style="color:#006666;color:#800000;">0</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006666;color:#800000;">1</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">else</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">n * factorial<span style="color:#006600; font-weight:bold;">&#40;</span>n-<span style="color:#006666;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000; font-style:italic;">#</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">doctest: should work <span style="color:#9966CC; font-weight:bold;">for</span> <span style="color:#006666;color:#800000;">3</span>, too</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&gt;&gt; factorial<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;color:#800000;">3</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">=&gt; <span style="color:#006666;color:#800000;">6</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<blockquote><p>When the above code is run, i.e. with the command “rubydoctest factorial.rb”, it shows the following results:</p></blockquote>
<p>上記のコードを"rubydoctest factorial.rb"というコマンドで実行させると、以下のような結果が得られます。</p>
<div class="igBar"><span id="lruby-41"><a href="#" onclick="javascript:showPlainTxt('ruby-41'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-41">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">=== Testing 'factorial.<span style="color:#9900CC;">rb</span>'...</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9900CC;">OK</span>&nbsp; | factorial should give correct results <span style="color:#9966CC; font-weight:bold;">for</span> <span style="color:#006666;color:#800000;">0</span> to <span style="color:#006666;color:#800000;">3</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">OK&nbsp; | should work <span style="color:#9966CC; font-weight:bold;">for</span> <span style="color:#006666;color:#800000;">3</span>, too</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006666;color:#800000;">6</span> comparisons | <span style="color:#006666;color:#800000;">2</span> doctests | <span style="color:#006666;color:#800000;">0</span> failures | <span style="color:#006666;color:#800000;">0</span> errors </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<blockquote><p>You can do multiline statements like this, as long as the indentation level is greater than the &gt;&gt; start line (note that ‘end’ is at an unnatural indentation level):</p></blockquote>
<p>インデントの階層をテスト開始行の"&gt;&gt;"よりも深くしておけば、複数行にまたがってテストコードを記述することもできます。<br />
(メモ:インデントの階層が条件に合わないとき、複数行コードの終了とみなします)</p>
<div class="igBar"><span id="lruby-42"><a href="#" onclick="javascript:showPlainTxt('ruby-42'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-42">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000; font-style:italic;">#&gt;&gt; all_true = true</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000; font-style:italic;">#&gt;&gt; 3.times do |t|</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000; font-style:italic;">#&nbsp; &nbsp; &nbsp; all_true &amp;&amp;= (a[t] == b[t])</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000; font-style:italic;">#&nbsp; &nbsp; &nbsp; end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000; font-style:italic;">#&gt;&gt; all_true</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000; font-style:italic;"># =&gt; true </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<blockquote><p>Here’s a more advanced usage example, in a Rails model, book.rb:</p></blockquote>
<p>以下にRailsのモデルクラス"book.rb"を使ってさらに進んだ使用例を示します。</p>
<div class="igBar"><span id="lruby-43"><a href="#" onclick="javascript:showPlainTxt('ruby-43'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-43">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#CC0066; font-weight:bold;">require</span> File.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span>File.<span style="color:#9900CC;">dirname</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">__FILE__</span><span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#996600;">".."</span>, <span style="color:#996600;">"rubydoctest_helper"</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">class</span> Book &lt;ActiveRecord::Base</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">=== Description</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Extension <span style="color:#9966CC; font-weight:bold;">for</span> sections <span style="color:#9966CC; font-weight:bold;">and</span> chapters collections</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">e.<span style="color:#9900CC;">g</span>. <span style="color:#9900CC;">book</span>.<span style="color:#9900CC;">sections</span>.<span style="color:#9900CC;">approved_only</span>, book.<span style="color:#9900CC;">chapters</span>.<span style="color:#9900CC;">dirty</span>!</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">=== Tests</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">doctest: Sample data should provide an example of a book with both approved</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">and</span> unapproved sections.</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&gt;&gt; v_a_books = Book.<span style="color:#9900CC;">varied</span><span style="color:#006600; font-weight:bold;">&#40;</span>:approved<span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&gt;&gt; v_a_books.<span style="color:#9900CC;">size</span>&gt; <span style="color:#006666;color:#800000;">0</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">=&gt; <span style="color:#0000FF; font-weight:bold;">true</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">doctest: Sample data should provide an example of a book with both dirty</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">and</span> non-dirty sections.</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&gt;&gt; v_d_books = Book.<span style="color:#9900CC;">varied</span><span style="color:#006600; font-weight:bold;">&#40;</span>:dirty<span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&gt;&gt; v_d_books.<span style="color:#9900CC;">size</span>&gt; <span style="color:#006666;color:#800000;">0</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">=&gt; <span style="color:#0000FF; font-weight:bold;">true</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">module</span> SectionExtension</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">=== Tests</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">doctest: Book<span style="color:#008000; font-style:italic;">#sections.approved_only should return approved sections,</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">including first-level sections, i.<span style="color:#9900CC;">e</span> “chapters”.</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&gt;&gt; s = v_a_books.<span style="color:#9900CC;">first</span>.<span style="color:#9900CC;">sections</span>.<span style="color:#9900CC;">approved_only</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&gt;&gt; s.<span style="color:#9900CC;">size</span>&gt; <span style="color:#006666;color:#800000;">1</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">=&gt; <span style="color:#0000FF; font-weight:bold;">true</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&gt;&gt; s.<span style="color:#9900CC;">any</span>?<span style="color:#006600; font-weight:bold;">&#123;</span> |t| t.<span style="color:#9900CC;">approved</span>? <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">=&gt; <span style="color:#0000FF; font-weight:bold;">true</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&gt;&gt; s.<span style="color:#9900CC;">any</span>?<span style="color:#006600; font-weight:bold;">&#123;</span> |t| !t.<span style="color:#9900CC;">approved</span>? <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">=&gt; <span style="color:#0000FF; font-weight:bold;">false</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">def</span> approved_only</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#CC0066; font-weight:bold;">select</span><span style="color:#006600; font-weight:bold;">&#123;</span> |s| s.<span style="color:#9900CC;">approved</span>? <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">=== Tests</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">doctest: Book<span style="color:#008000; font-style:italic;">#sections.unapproved_only should return unapproved sections,</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">including first-level sections, i.<span style="color:#9900CC;">e</span> “chapters”.</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&gt;&gt; s = v_a_books.<span style="color:#9900CC;">first</span>.<span style="color:#9900CC;">sections</span>.<span style="color:#9900CC;">unapproved_only</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&gt;&gt; s.<span style="color:#9900CC;">size</span>&gt; <span style="color:#006666;color:#800000;">1</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">=&gt; <span style="color:#0000FF; font-weight:bold;">true</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&gt;&gt; s.<span style="color:#9900CC;">any</span>?<span style="color:#006600; font-weight:bold;">&#123;</span> |t| t.<span style="color:#9900CC;">approved</span>? <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">=&gt; <span style="color:#0000FF; font-weight:bold;">false</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&gt;&gt; s.<span style="color:#9900CC;">any</span>?<span style="color:#006600; font-weight:bold;">&#123;</span> |t| !t.<span style="color:#9900CC;">approved</span>? <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">=&gt; <span style="color:#0000FF; font-weight:bold;">true</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">def</span> unapproved_only</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#CC0066; font-weight:bold;">select</span><span style="color:#006600; font-weight:bold;">&#123;</span> |s| !s.<span style="color:#9900CC;">approved</span>? <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">=== Tests</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">doctest: Book<span style="color:#008000; font-style:italic;">#sections.dirty! should set the dirty flag for sections</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">specific to the particular book it was called on.</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&gt;&gt; book = v_d_books.<span style="color:#9900CC;">first</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&gt;&gt; <span style="color:#006600; font-weight:bold;">&#40;</span>yes = book.<span style="color:#9900CC;">sections</span>.<span style="color:#9900CC;">count</span><span style="color:#006600; font-weight:bold;">&#40;</span>:conditions =&gt; <span style="color:#006600; font-weight:bold;">&#91;</span>“dirty = ?”, <span style="color:#0000FF; font-weight:bold;">true</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>&gt; <span style="color:#006666;color:#800000;">0</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">=&gt; <span style="color:#0000FF; font-weight:bold;">true</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&gt;&gt; <span style="color:#006600; font-weight:bold;">&#40;</span>no = book.<span style="color:#9900CC;">sections</span>.<span style="color:#9900CC;">count</span><span style="color:#006600; font-weight:bold;">&#40;</span>:conditions =&gt; <span style="color:#006600; font-weight:bold;">&#91;</span>“dirty = ?”, <span style="color:#0000FF; font-weight:bold;">false</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>&gt; <span style="color:#006666;color:#800000;">0</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">=&gt; <span style="color:#0000FF; font-weight:bold;">true</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&gt;&gt; v_d_books.<span style="color:#9900CC;">first</span>.<span style="color:#9900CC;">sections</span>.<span style="color:#9900CC;">dirty</span>!</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&gt;&gt; book.<span style="color:#9900CC;">sections</span>.<span style="color:#9900CC;">count</span><span style="color:#006600; font-weight:bold;">&#40;</span>:conditions =&gt; <span style="color:#006600; font-weight:bold;">&#91;</span>“dirty = ?”, <span style="color:#0000FF; font-weight:bold;">true</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">=&gt; yes + no</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&gt;&gt; book.<span style="color:#9900CC;">sections</span>.<span style="color:#9900CC;">count</span><span style="color:#006600; font-weight:bold;">&#40;</span>:conditions =&gt; <span style="color:#006600; font-weight:bold;">&#91;</span>“dirty = ?”, <span style="color:#0000FF; font-weight:bold;">false</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">=&gt; <span style="color:#006666;color:#800000;">0</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">def</span> dirty!</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">update_all</span> <span style="color:#006600; font-weight:bold;">&#91;</span>“dirty = ?”, <span style="color:#0000FF; font-weight:bold;">true</span><span style="color:#006600; font-weight:bold;">&#93;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">has_many :sections,&nbsp; :extend =&gt; SectionExtension</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">=== Description</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Find books with a diverse set of sections with a certain property.</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">=== Examples</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">@see SectionExtension <span style="color:#9966CC; font-weight:bold;">module</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">varied</span><span style="color:#006600; font-weight:bold;">&#40;</span>field, min = <span style="color:#006666;color:#800000;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">find_by_sql \</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">“<span style="color:#CC0066; font-weight:bold;">SELECT</span> * FROM books WHERE id <span style="color:#9966CC; font-weight:bold;">IN</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">SELECT</span> book_id FROM sections</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">GROUP BY book_id</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">HAVING COUNT&gt;= <span style="color:#008000; font-style:italic;">#{min})”</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<blockquote><p>See the Rails Usage page for more information about the rubydoctest_helper script mentioned above.</p></blockquote>
<p>上記のコード中で参照しているスクリプト"rubydoctest_helper"についての詳しい説明は<a href="https://github.com/tablatom/rubydoctest/wiki/Rails-Usage">ページ"Rails Usage"</a>を参照してください。</p>
<blockquote><p>In each case, you run your tests using the rubydoctest command-line tool, e.g.:</p></blockquote>
<p>どちらの場合においても、コマンドラインツール"rubydoctest"を使ってテストを実行します。</p>
<div class="igBar"><span id="lruby-44"><a href="#" onclick="javascript:showPlainTxt('ruby-44'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-44">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$ rubydoctest doc/sample.<span style="color:#9900CC;">doctest</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$ rubydoctest factorial.<span style="color:#9900CC;">rb</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$ rubydoctest app/models/book.<span style="color:#9900CC;">rb</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<blockquote><p>Note also that you can use =begin and =end to suround the tests, ex: factorial.rb</p></blockquote>
<p>埋め込みドキュメント(rubyコード中、=beginと=endに囲まれている部分)内に記述できる事を、先ほどのアプリケーション"factorial.rb"を例に付記しておきます。<br />
訳注:埋め込みドキュメントについては下記を参照してください。<br />
<a href="http://doc.okkez.net/static/192/doc/spec=2flexical.html#embed">埋め込みドキュメント &lt; 字句構造 &lt; Ruby 1.9.2 リファレンスマニュアル</a></p>
<div class="igBar"><span id="lruby-45"><a href="#" onclick="javascript:showPlainTxt('ruby-45'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-45">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">def</span> factorial<span style="color:#006600; font-weight:bold;">&#40;</span>n<span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">if</span> n == <span style="color:#006666;color:#800000;">0</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006666;color:#800000;">1</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">else</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">n * factorial<span style="color:#006600; font-weight:bold;">&#40;</span>n-<span style="color:#006666;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="">=begin</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="">doctest: fact</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="">&gt;&gt; factorial(0)</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="">=&gt; 1</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="">=end </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<blockquote><p>And that tests can be interspersed with comments, a la</p></blockquote>
<p>そしてこのテストコードには、以下のようにコメントをちりばめることができます。</p>
<div class="igBar"><span id="lruby-46"><a href="#" onclick="javascript:showPlainTxt('ruby-46'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-46">
<div class="ruby">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">doctest: factial <span style="color:#006666;color:#800000;">0</span> is <span style="color:#006666;color:#800000;">1</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&gt;&gt; factorial<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;color:#800000;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">=&gt; <span style="color:#006666;color:#800000;">1</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000; font-style:italic;">#</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">These numbers grow much larger with a higher n:</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000; font-style:italic;">#</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&gt;&gt; factorial<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;color:#800000;">10</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">=&gt; <span style="color:#006666;color:#800000;">3628800</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<blockquote><p>And since your test code and comments will appear in the rdocs, you’ve made instant documentation for parameters to your functions.</p></blockquote>
<p>そして、あなたのテストコードはRDoc上にも出力されるため、メソッド(など)についての使用例のドキュメントを作成したことにもなるのです。</p>
<blockquote><p>And all it cost was a copy and past from an irb session.</p></blockquote>
<p>そのドキュメントはirbセッションからコピーペーストする労力しかかかっていないのです。</p>
<blockquote><p>Last edited by canadaduane, September 15, 2010</p></blockquote>
<p>最終更新： 2010/09/15 <a href="https://github.com/canadaduane">canadaduane</a></p>
]]></content:encoded>
			<wfw:commentRss>http://freestyle.nvo.jp/archives/931/feed</wfw:commentRss>
		</item>
	</channel>
</rss>

