<?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>Fri, 03 Feb 2012 12:47:37 +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/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>マジックナンバー&#8221;37.5782&#8243;がよくわからないので隠蔽するために、処理をメソッド化しました。(※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エンジニア界隈では、 &#8220;Play Framework&#8221; が最近注目されているらしいです。</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-2"><a href="#" onclick="javascript:showPlainTxt('ruby-2'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-2">
<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-11"><a href="#" onclick="javascript:showPlainTxt('ruby-11'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-11">
<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-12"><a href="#" onclick="javascript:showPlainTxt('ruby-12'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-12">
<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-13"><a href="#" onclick="javascript:showPlainTxt('ruby-13'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-13">
<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-14"><a href="#" onclick="javascript:showPlainTxt('ruby-14'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-14">
<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-15"><a href="#" onclick="javascript:showPlainTxt('ruby-15'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-15">
<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-16"><a href="#" onclick="javascript:showPlainTxt('ruby-16'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-16">
<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-17"><a href="#" onclick="javascript:showPlainTxt('ruby-17'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-17">
<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-18"><a href="#" onclick="javascript:showPlainTxt('ruby-18'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-18">
<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>
		<item>
		<title>レーベンシュタイン距離で文字列の類似度を測る</title>
		<link>http://freestyle.nvo.jp/archives/925</link>
		<comments>http://freestyle.nvo.jp/archives/925#comments</comments>
		<pubDate>Fri, 18 Nov 2011 05:06:33 +0000</pubDate>
		<dc:creator>山平</dc:creator>
		
		<category><![CDATA[技術情報]]></category>

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

		<guid isPermaLink="false">http://freestyle.nvo.jp/?p=925</guid>
		<description><![CDATA[過去にネットで見かけてその存在だけは知っていたレーベンシュタイン距離について追ってみます。
編集距離 (Levenshtein Distance) - naoyaのはてなダイアリー
すでにrubyで実装されている方がいら [...]]]></description>
			<content:encoded><![CDATA[<p>過去にネットで見かけてその存在だけは知っていたレーベンシュタイン距離について追ってみます。<br />
<a href="http://d.hatena.ne.jp/naoya/20090329/1238307757">編集距離 (Levenshtein Distance) - naoyaのはてなダイアリー</a></p>
<p>すでにrubyで実装されている方がいらっしゃったので、それを使うことにします。<br />
<a href="http://d.hatena.ne.jp/kenkitii/20090204/ruby_levenshtein_distance">ruby でレーベンシュタイン距離（編集距離）の計算 - Moderation is a fatal thing. Nothing succeeds like excess.</a></p>
<p>（私的な）使い勝手向上のために以下2点の修正を加えています。</p>
<ol>
<li>Stringクラスを再オープンしてメソッドを定義</li>
<li>メソッド名をldに変更</li>
</ol>
<p><span id="more-925"></span></p>
<p>では早速動作をチェックしてみます。</p>
<blockquote><p>"氷山の一角".ld "天山の一撃"<br />
=&gt; 6<br />
"ヘリコプター".ld "ヘコリプター"<br />
=&gt; 4<br />
"じゃがいも".ld "がじゃいも"<br />
=&gt; 5</p></blockquote>
<p>良さそうな気もしますが、3つとも2文字違いの割に結果（の距離）が長い気がします。<br />
と思ったら、これは恐らくRuby1.9向けの実装ですね。</p>
<p><a href="http://doc.ruby-lang.org/ja/1.8.7/method/String/i/=5b=5d.html">Ruby 1.8.7 リファレンスマニュアル &gt; ライブラリ一覧 &gt; 組み込みライブラリ &gt; Stringクラス &gt; [] </a></p>
<blockquote><p><span style="color: #ff0000;"><strong>nth 番目のバイトを整数 (文字コード) で返します。</strong></span><br />
nth が負の場合は文字列の末尾から数えます。<br />
つまり、 self.size + nth 番目のバイトを返します。</p></blockquote>
<p><a href="http://doc.okkez.net/static/192/method/String/i/=5b=5d.html">Ruby 1.9.2 リファレンスマニュアル &gt; ライブラリ一覧 &gt; 組み込みライブラリ &gt; Stringクラス &gt; [] </a></p>
<blockquote><p><span style="color: #0000ff;"><strong>nth 番目の文字を返します。</strong></span><br />
nth が負の場合は文字列の末尾から数えます。<br />
つまり、 self.size + nth 番目の文字を返します。</p></blockquote>
<p>1.8系の文字列は内部的にはただのバイト列、1.9系からは内部的にも文字の列なので、私の環境（1.8.7）では全角文字のバイト数ぶんカウントされているものと思われます。<br />
なので、文字列ではなく正規表現で分割して文字配列で処理させるように変更します。<br />
※uオプションを利用していますので、UTF-8以外は正しく処理しません！<br />
<a href="http://doc.okkez.net/static/191/doc/spec=2fliteral.html#regexp">Ruby 1.9.1 リファレンスマニュアル &gt; spec/literal # 正規表現リテラル</a></p>
<p>では、改めて動作をチェックします。</p>
<blockquote><p>"氷山の一角".ld "天山の一撃"<br />
=&gt; 2<br />
"ヘリコプター".ld "ヘコリプター"<br />
=&gt; 2<br />
"じゃがいも".ld "がじゃいも"<br />
=&gt; 2</p></blockquote>
<p>これで予想通りの結果になりました。</p>
<p>以下が今回手を入れたソースです。</p>
<div class="igBar"><span id="lruby-20"><a href="#" onclick="javascript:showPlainTxt('ruby-20'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-20">
<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;">class</span> <span style="color:#CC0066; font-weight:bold;">String</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> ld<span style="color:#006600; font-weight:bold;">&#40;</span>comp<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;">str1, str2 = <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span>//u<span style="color:#006600; font-weight:bold;">&#41;</span>, comp.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span>//u<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;"># 1.8系対応(*UTF8!)</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">col, row = str1.<span style="color:#9900CC;">size</span> + <span style="color:#006666;color:#800000;">1</span>, str2.<span style="color:#9900CC;">size</span> + <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;">d = row.<span style="color:#9900CC;">times</span>.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</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;">&#123;</span>|a, i| a &lt;&lt;<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;color:#800000;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span> * col <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;">col.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span>|i| d<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;color:#800000;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span>i<span style="color:#006600; font-weight:bold;">&#93;</span> = i <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;">row.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span>|i| d<span style="color:#006600; font-weight:bold;">&#91;</span>i<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;color:#800000;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span> = i <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;">str1.<span style="color:#9900CC;">size</span>.<span style="color:#9900CC;">times</span> <span style="color:#9966CC; font-weight:bold;">do</span> |i1|</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;">str2.<span style="color:#9900CC;">size</span>.<span style="color:#9900CC;">times</span> <span style="color:#9966CC; font-weight:bold;">do</span> |i2|</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">cost = str1<span style="color:#006600; font-weight:bold;">&#91;</span>i1<span style="color:#006600; font-weight:bold;">&#93;</span> == str2<span style="color:#006600; font-weight:bold;">&#91;</span>i2<span style="color:#006600; font-weight:bold;">&#93;</span> ? <span style="color:#006666;color:#800000;">0</span> : <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;">x, y = i1 + <span style="color:#006666;color:#800000;">1</span>, i2 + <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;">d<span style="color:#006600; font-weight:bold;">&#91;</span>y<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span>x<span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#006600; font-weight:bold;">&#91;</span>d<span style="color:#006600; font-weight:bold;">&#91;</span>y<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span>x-<span style="color:#006666;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>+<span style="color:#006666;color:#800000;">1</span>, d<span style="color:#006600; font-weight:bold;">&#91;</span>y-<span style="color:#006666;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span>x<span style="color:#006600; font-weight:bold;">&#93;</span>+<span style="color:#006666;color:#800000;">1</span>, d<span style="color:#006600; font-weight:bold;">&#91;</span>y-<span style="color:#006666;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span>x-<span style="color:#006666;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>+cost<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">min</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;">d<span style="color:#006600; font-weight:bold;">&#91;</span>str2.<span style="color:#9900CC;">size</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span>str1.<span style="color:#9900CC;">size</span><span style="color:#006600; font-weight:bold;">&#93;</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>
<p>当たり前ですが、このアルゴリズムは単語同士の比較には有用でも、単語と文章の比較には使えません。</p>
<blockquote><p>"七転八倒".ld "七転び八起き"<br />
=&gt; 3<br />
"七転八倒".ld "人生は七転び八起きの精神だ"<br />
=&gt; 10</p></blockquote>
<p>似ているかもしれない2つの単語を抽出するところまでをどうすればよいのか、課題は尽きません。</p>
<p>以上です。</p>
]]></content:encoded>
			<wfw:commentRss>http://freestyle.nvo.jp/archives/925/feed</wfw:commentRss>
		</item>
		<item>
		<title>N-gramで文字列の類似度を測る</title>
		<link>http://freestyle.nvo.jp/archives/919</link>
		<comments>http://freestyle.nvo.jp/archives/919#comments</comments>
		<pubDate>Fri, 04 Nov 2011 00:33:17 +0000</pubDate>
		<dc:creator>山平</dc:creator>
		
		<category><![CDATA[技術情報]]></category>

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

		<guid isPermaLink="false">http://freestyle.nvo.jp/?p=919</guid>
		<description><![CDATA[とある2つの文字列がどれぐらい似ているのかを比較するにはどうすればいいのかを調べる必要があったので、ネットで検索してみました。
perlにはString::Trigramというモジュールがあるようです。
String:: [...]]]></description>
			<content:encoded><![CDATA[<p>とある2つの文字列がどれぐらい似ているのかを比較するにはどうすればいいのかを調べる必要があったので、ネットで検索してみました。<br />
perlには<a href="http://search.cpan.org/dist/String-Trigram/Trigram.pm">String::Trigram</a>というモジュールがあるようです。</p>
<p><a href="http://blog.livedoor.jp/techblog/archives/64737211.html">String::Trigram でテキストの類似度を測る</a></p>
<p>rubyで同じようなものが見つからなかったので、取り急ぎ作って見ました。</p>
<p><span id="more-919"></span></p>
<div class="igBar"><span id="lruby-22"><a href="#" onclick="javascript:showPlainTxt('ruby-22'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-22">
<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;">#類似度の比較</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;">class</span> <span style="color:#CC0066; font-weight:bold;">String</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> ngram<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">string</span>, part_len = <span style="color:#006666;color:#800000;">3</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:#CC0066; font-weight:bold;">string</span> = <span style="color:#CC0066; font-weight:bold;">string</span>.<span style="color:#9900CC;">dup</span>.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span>/<span style="color:#006600; font-weight:bold;">&#91;</span>\s\n　<span style="color:#006600; font-weight:bold;">&#93;</span>+/u, <span style="color:#996600;">""</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;">strlen = <span style="color:#CC0066; font-weight:bold;">string</span>.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span>//u<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">length</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">points = <span style="color:#006600; font-weight:bold;">&#91;</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;">source = <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span>/<span style="color:#006600; font-weight:bold;">&#91;</span>\s\n　<span style="color:#006600; font-weight:bold;">&#93;</span>+/u, <span style="color:#996600;">""</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;">srcarr = source.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span>//u<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;">sourcelen = srcarr.<span style="color:#9900CC;">length</span></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;">return</span> <span style="color:#0000FF; font-weight:bold;">nil</span> <span style="color:#9966CC; font-weight:bold;">if</span> part_len&gt; sourcelen <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:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">nil</span> <span style="color:#9966CC; font-weight:bold;">if</span> part_len&gt; strlen <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:#006600; font-weight:bold;">&#91;</span><span style="color:#CC0066; font-weight:bold;">string</span>, source<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span>|cmpstr|</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;">counter = <span style="color:#006666;color:#800000;">0</span>.<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;"><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;color:#800000;">0</span>..<span style="color:#006600; font-weight:bold;">&#40;</span>sourcelen - part_len<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">each</span><span style="color:#006600; font-weight:bold;">&#123;</span>|start|<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;">part = srcarr<span style="color:#006600; font-weight:bold;">&#91;</span>start, part_len<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">""</span><span style="color:#006600; font-weight:bold;">&#41;</span> <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;">hit = cmpstr.<span style="color:#9900CC;">scan</span><span style="color:#006600; font-weight:bold;">&#40;</span>/<span style="color:#008000; font-style:italic;">#{Regexp.quote(part)}/).size</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;">counter += hit</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;">&#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;">points &lt;&lt;counter</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;">&#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;">points.<span style="color:#9900CC;">shift</span> / points.<span style="color:#9900CC;">shift</span> <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:#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>
<p><a href="http://search.cpan.org/dist/String-Trigram/Trigram.pm">String::Trigram</a>の動作を見ていないので同じ挙動かどうかは確認できていませんが、String自身と比較対象文字がどれくらい似ているのかを百分率で返します。<br />
何文字ずつ比較するかを設定できますが、比較できないような長さを設定するとnilが返ります。</p>
<p>動作確認として、上の<a href="http://blog.livedoor.jp/techblog/archives/64737211.html">livedoor Techブログ</a>さまで比較している文字列を使って実行してみましょう。</p>
<blockquote><p>"ライブドアでブログを書く".ngram("ブログをライブドアで書く")<br />
=&gt; 0.6<br />
"梅雨（ばいう、つゆ）とは、北海道と小笠原諸島を除く日本や朝鮮半島南部、華南や華中の沿海部や台湾において見られる特有の気象で、5月から7月半ばにかけて毎年めぐってくる、雨の多い期間のこと。梅雨の時季が始まることを梅雨入り、梅雨が終わって夏になることを梅雨明けと言い、日本では、各地の地方気象台・気象庁が梅雨入り・梅雨明けの発表をする。".ngram("「梅雨」北海道と小笠原諸島を除く日本において見られる特有の気象で、5月から7月半ばにかけて毎年めぐってくる、雨の多い期間のこと。梅雨に入ることを梅雨入り、梅雨が終わって夏になることを梅雨明けと言い、日本各地の地方気象台・気象庁が梅雨入り・梅雨明けの発表をする。")<br />
=&gt; 0.733333333333333</p></blockquote>
<p>長い文字列の結果がちょっと違いますね。。。<br />
念のため、もう少し比べてみます。</p>
<blockquote><p>"鳴かぬなら殺してしまえホトトギス".ngram("鳴かぬなら鳴くまで待とうホトトギス")<br />
=&gt; 0.428571428571429<br />
"鳴かぬなら鳴くまで待とうホトトギス".ngram("鳴かぬなら殺してしまえホトトギス")<br />
=&gt; 0.4</p></blockquote>
<blockquote><p>"織田信長は、桶狭間で今川義元を破り、天下統一の足がかりを作った".ngram("徳川家康は、二度に渡る大阪の陣で豊臣家を滅ぼし、名実共に天下人となった")<br />
=&gt; 0.0<br />
"織田信長は、桶狭間で今川義元を破り、天下統一の足がかりを作った".ngram("徳川家康は、二度に渡る大阪の陣で豊臣家を滅ぼし、名実共に天下人となった", 2)<br />
=&gt; 0.1<br />
"徳川家康は、二度に渡る大阪の陣で豊臣家を滅ぼし、名実共に天下人となった".ngram("織田信長は、桶狭間で今川義元を破り、天下統一の足がかりを作った")<br />
=&gt; 0.0<br />
"徳川家康は、二度に渡る大阪の陣で豊臣家を滅ぼし、名実共に天下人となった".ngram("織田信長は、桶狭間で今川義元を破り、天下統一の足がかりを作った", 2)<br />
=&gt; 0.0882352941176471</p></blockquote>
<p>実用に耐える程度には抽出できているようなので今回はここまでです。</p>
]]></content:encoded>
			<wfw:commentRss>http://freestyle.nvo.jp/archives/919/feed</wfw:commentRss>
		</item>
		<item>
		<title>Web Storageを使ってみる</title>
		<link>http://freestyle.nvo.jp/archives/907</link>
		<comments>http://freestyle.nvo.jp/archives/907#comments</comments>
		<pubDate>Mon, 17 Oct 2011 06:42:25 +0000</pubDate>
		<dc:creator>fujii</dc:creator>
		
		<category><![CDATA[技術情報]]></category>

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

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

		<guid isPermaLink="false">http://freestyle.nvo.jp/?p=907</guid>
		<description><![CDATA[HTML5と共に標準化が進められているAPIの中にWeb Storageがあります。
Web Storageを利用すると、ローカルディスク上にデータの保存が可能となります。
Cookieと異なり有効期限がないので、データ [...]]]></description>
			<content:encoded><![CDATA[<p>HTML5と共に標準化が進められているAPIの中に<strong>Web Storage</strong>があります。<br />
<strong>Web Storage</strong>を利用すると、ローカルディスク上にデータの保存が可能となります。<br />
Cookieと異なり有効期限がないので、データを永続的に保存することができます。</p>
<p><strong>Web Storage</strong>には<strong>ローカルストレージ</strong>、<strong>セッションストレージ</strong>の二種類があります。</p>
<ul>
<li><strong>ローカルストレージ</strong>は永続的にデータの保存が可能</li>
<li><strong>セッションストレージ</strong>はブラウザを開いている間のみデータの保存が可能</li>
</ul>
<p>使い方は簡単で、データの読み書きは以下になります。<br />
<span id="more-907"></span></p>
<p>[保存]</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;">localStorage.<span style="color: #006600;">setItem</span><span style="color: #66cc66;">&#40;</span>key, value<span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>[読み込み]</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;">ret = localStorage.<span style="color: #006600;">getItem</span><span style="color: #66cc66;">&#40;</span>key<span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>上記のように、メソッドを呼び出してデータの読み書きできますが、配列、プロパティ形式でも利用可能です。</p>
<div class="igBar"><span id="ljavascript-29"><a href="#" onclick="javascript:showPlainTxt('javascript-29'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-29">
<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;">localStorage.<span style="color: #006600;">setItem</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">"key_dayo"</span>, <span style="color: #3366CC;">"value_dayo"</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">localStorage.<span style="color: #006600;">key_dayo</span> = <span style="color: #3366CC;">"value_dayo"</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;">localStorage<span style="color: #66cc66;">&#91;</span><span style="color: #3366CC;">"key_dayo"</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #3366CC;">"value_dayo"</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>このローカルストレージを利用し、下の画像のような簡単なサンプル(簡易メモ)を作成してみました。</p>
<p><a href="http://freestyle.nvo.jp/wp-content/uploads/2011/10/easy_memo.png"><img class="alignnone size-medium wp-image-911" title="easy_memo" src="http://freestyle.nvo.jp/wp-content/uploads/2011/10/easy_memo-300x186.png" alt="easy_memo" width="300" height="186" /></a></p>
<p>「保存」ボタンを押すことで、テキストエリア内に記述した文字列を<strong>ローカルストレージ</strong>に保存します。<br />
「読込」 ボタンを押すと<strong>ローカルストレージ</strong>に保存した、文字列をテキストエリア内に表示することができます。</p>
<div class="igBar"><span id="lhtml-30"><a href="#" onclick="javascript:showPlainTxt('html-30'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">HTML:</span>
<div id="html-30">
<div class="html">
<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: #009900;"><a href="http://december.com/html/4/element/h1.html"><span style="color: #000000; font-weight: bold;">&lt;h1&gt;</span></a></span>簡易メモ<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/h1&gt;</span></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;"><a href="http://december.com/html/4/element/textarea.html"><span style="color: #000000; font-weight: bold;">&lt;textarea</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"txtMemo"</span> <span style="color: #000066;">cols</span>=<span style="color: #ff0000;">"50"</span> <span style="color: #000066;">rows</span>=<span style="color: #ff0000;">"10"</span><span style="color: #000000; font-weight: bold;">&gt;</span></a></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/textarea&gt;</span></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: #009900;"><a href="http://december.com/html/4/element/input.html"><span style="color: #000000; font-weight: bold;">&lt;input</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"saveMemo"</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"saveMemo"</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"button"</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">"保存"</span> /<span style="color: #000000; font-weight: bold;">&gt;</span></a></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;"><a href="http://december.com/html/4/element/input.html"><span style="color: #000000; font-weight: bold;">&lt;input</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"readMemo"</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"readMemo"</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"button"</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">"読込"</span> /<span style="color: #000000; font-weight: bold;">&gt;</span></a></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: #009900;"><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">&lt;script</span></a> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"text/javascript"</span><span style="color: #000000; font-weight: bold;">&gt;</span></a></span><span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!--</span></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;">&nbsp; &nbsp; &nbsp; &nbsp; (function() {</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: #808080; font-style: italic;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!localStorage) {</span></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;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert(&quot;ローカルストレージが利用できません。&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: #808080; font-style: italic;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;</span></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;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"></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;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var objMemo = document.getElementById(&quot;txtMemo&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: #808080; font-style: italic;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getLocalStorageMemo();</span></div>
</li>
<li style="font-weight: bold;color:#26536A;"></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: #808080; font-style: italic;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById(&quot;saveMemo&quot;).addEventListener(&quot;click&quot;, function() {</span></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;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; addEventListener</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: #808080; font-style: italic;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!localStorage) return;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;"></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: #808080; font-style: italic;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var objMemo = document.getElementById(&quot;txtMemo&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: #808080; font-style: italic;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; localStorage.setItem(&quot;KeyMemo&quot;, objMemo.value);</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"></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;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert(&quot;ローカルストレージに登録しました。&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: #808080; font-style: italic;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; objMemo.value = &quot;&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: #808080; font-style: italic;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }, true);</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"></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;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById(&quot;readMemo&quot;).addEventListener(&quot;click&quot;, function() {</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: #808080; font-style: italic;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getLocalStorageMemo();</span></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;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }, true);</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"></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;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; function getLocalStorageMemo() {</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: #808080; font-style: italic;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var strTodo = localStorage.getItem(&quot;KeyMemo&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: #808080; font-style: italic;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (strTodo) {</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: #808080; font-style: italic;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; objMemo.value = strTodo;</span></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;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</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: #808080; font-style: italic;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</span></div>
</li>
<li style="font-weight: bold;color:#26536A;"></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: #808080; font-style: italic;">&nbsp; &nbsp; &nbsp; &nbsp; })();</span></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;">// --&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span></span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p><strong>Web Storage</strong>はCookieと同じく手軽にデータを保存できる便利なAPIです。<br />
<strong> Web Strage</strong>を利用することで、使い勝手のいいWebページを作成できると思います。<br />
ブラウザの対応状況がマチマチなので、その辺りを考慮して作成する必要がありますが、ぜひ利用してみてはいかがでしょうか。</p>
]]></content:encoded>
			<wfw:commentRss>http://freestyle.nvo.jp/archives/907/feed</wfw:commentRss>
		</item>
		<item>
		<title>端末に表示するフォントの色を変更する</title>
		<link>http://freestyle.nvo.jp/archives/897</link>
		<comments>http://freestyle.nvo.jp/archives/897#comments</comments>
		<pubDate>Mon, 05 Sep 2011 00:40:42 +0000</pubDate>
		<dc:creator>山平</dc:creator>
		
		<category><![CDATA[技術情報]]></category>

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

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

		<guid isPermaLink="false">http://freestyle.nvo.jp/?p=897</guid>
		<description><![CDATA[Unix系のアプリケーションの場合、基本的に本体はコマンド群で構成されています。
時々CUIフロントエンドがついてきたりしますが、標準でGUIのフロントエンドがついてくるのは稀なように感じます。
今回はCUIのフロントエ [...]]]></description>
			<content:encoded><![CDATA[<p>Unix系のアプリケーションの場合、基本的に本体はコマンド群で構成されています。<br />
時々CUIフロントエンドがついてきたりしますが、標準でGUIのフロントエンドがついてくるのは稀なように感じます。</p>
<p>今回はCUIのフロントエンドを作りたい場合の話になります。<br />
端末上に表示する文字の色や太さを変更するにはどうすればいいのかを調べていて、以下のようなサイトを見つけました。</p>
<p><span id="more-897"></span></p>
<p><a href="http://d.hatena.ne.jp/maru_cc/20080216/1203166254">phpのバッチでターミナル上で色をつける方法</a></p>
<p>フムフム、「制御コード＋色指定」でいけるとのことなので試してみましょう。</p>
<div class="igBar"><span id="lruby-32"><a href="#" onclick="javascript:showPlainTxt('ruby-32'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-32">
<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;">@color = <span style="color:#006600; font-weight:bold;">&#123;</span> :none=&gt;<span style="color:#996600;">"[0m"</span>, :red=&gt;<span style="color:#996600;">"[31m"</span>, :blue=&gt;<span style="color:#996600;">"[34m"</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;">def</span> tcs<span style="color:#006600; font-weight:bold;">&#40;</span>color, str<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#91;</span>0x1b<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">pack</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">"c*"</span><span style="color:#006600; font-weight:bold;">&#41;</span> + @color<span style="color:#006600; font-weight:bold;">&#91;</span>color<span style="color:#006600; font-weight:bold;">&#93;</span> + str; <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:#006600; font-weight:bold;">&#91;</span>:red, :blue, :none<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">each</span><span style="color:#006600; font-weight:bold;">&#123;</span>|c| <span style="color:#CC0066; font-weight:bold;">puts</span> tcs<span style="color:#006600; font-weight:bold;">&#40;</span>c, <span style="color:#996600;">"colorful string"</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p><a href="../wp-content/uploads/2011/09/colorful_terminal.png"><img class="aligncenter size-medium wp-image-901" title="colorful_terminal" src="../wp-content/uploads/2011/09/colorful_terminal-300x116.png" alt="colorful_terminal" width="300" height="116" /></a></p>
<p>あっさりできました。<br />
ただ、以降の文字も同じ指定を引き継いでしまうので、色指定の最後にデフォルトの設定に戻してやる必要があります。<br />
もう一点、過去にemacsのshellモードで色認識しなかったことがあり、制御文字がそのまま表示されてしまってずいぶん見難い状態になったことがあります。<br />
色制御を行う前に、端末が色を認識できるかどうかのチェックが必要なようです。</p>
<p>調査を進めると、端末制御のデファクトスタンダード、Cursesライブラリというものがあるようです。<br />
Rubyもこれをラップしたライブラリを持っており、その中で色認識の可否も確認できるようです。</p>
<ul>
<li><a href="http://ja.wikipedia.org/wiki/Curses">Curses - Wikipedia</a></li>
<li><a href="http://doc.okkez.net/static/192/class/Curses.html">module Curses - Ruby 1.9.2 リファレンスマニュアル</a></li>
</ul>
<p>今日はここまでです。</p>
]]></content:encoded>
			<wfw:commentRss>http://freestyle.nvo.jp/archives/897/feed</wfw:commentRss>
		</item>
		<item>
		<title>今日が「毎月第2、第4木曜日の6日前」かどうかを判定する処理</title>
		<link>http://freestyle.nvo.jp/archives/891</link>
		<comments>http://freestyle.nvo.jp/archives/891#comments</comments>
		<pubDate>Fri, 19 Aug 2011 04:18:20 +0000</pubDate>
		<dc:creator>山平</dc:creator>
		
		<category><![CDATA[技術情報]]></category>

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

		<guid isPermaLink="false">http://freestyle.nvo.jp/?p=891</guid>
		<description><![CDATA[とある集まり（毎月第2、第4木曜日に開催）の出欠を確認する必要があり、念のため週末をはさんで連絡する役になっているのですが、正直、自信がありません。
遠くない将来、連絡を忘れてしまうことは火を見るよりも明らかなので、私よ [...]]]></description>
			<content:encoded><![CDATA[<p>とある集まり（毎月第2、第4木曜日に開催）の出欠を確認する必要があり、念のため週末をはさんで連絡する役になっているのですが、正直、自信がありません。<br />
遠くない将来、連絡を忘れてしまうことは火を見るよりも明らかなので、私よりも几帳面なCRONに監視してもらうことにします。</p>
<p>せっかく面倒臭そうな判定を作るので、今後も使えそうなものにしたいと思います。</p>
<p><span id="more-891"></span></p>
<h3>1.考え方</h3>
<p>タイトルでは、『今日が「毎月第2、第4木曜日の6日前」かどうか』という言い回しになっていますが、条件が少し変わると月を跨ぐので、ここを汎用的になるように注意します。<br />
例）15日前、第１木曜日、など<br />
そこで、意味を変えずに『今日の6日後が「第2、第4木曜日」かどうか』と読みかえて考えます。</p>
<p>当然、木曜日以外でも使えるようにするので、「指定月のX番目のY曜日かどうか」が分かるような仕組みを作る必要がありそうです。</p>
<p>下記のような比較ができるようにDateクラスを拡張してみます。<br />
今日+6日 == 指定年月→指定曜日→2番目</p>
<h3>2.実装</h3>
<p>指定年月の指定曜日のN番目を取得する、というと複雑な気がしますが、配列で実現できました。</p>
<div class="igBar"><span id="lruby-36"><a href="#" onclick="javascript:showPlainTxt('ruby-36'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-36">
<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;">#2011年8月の木曜日</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">ym201108thu = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">"2011-08-04"</span>, <span style="color:#996600;">"2011-08-11"</span>, <span style="color:#996600;">"2011-08-18"</span>, <span style="color:#996600;">"2011-08-25"</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:#008000; font-style:italic;">#2011年8月の第2木曜日（配列は0から数えるため、-1する）</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">ym201108thu<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#008000; font-style:italic;">#=&gt;&quot;2011-08-11&quot; </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>ということは、Dateインスタンス(指定年月)の指定曜日が配列で取得できれば解決できそうなので、以下のようにDateクラスにメソッドを追加します。</p>
<div class="igBar"><span id="lruby-37"><a href="#" onclick="javascript:showPlainTxt('ruby-37'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-37">
<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:#CC0066; font-weight:bold;">require</span> 'date'</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> Date</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">%w<span style="color:#006600; font-weight:bold;">&#91;</span>mondays tuesdays wednesdays thursdays fridays saturdays sundays<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">inject</span><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><span style="color:#006600; font-weight:bold;">&#123;</span>|wday, 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:#CC0066; font-weight:bold;">eval</span> <span style="color:#996600;">"def #{name} ; weekdays(#{wday});end"</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">wday += <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;"><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;">def</span> weekdays<span style="color:#006600; font-weight:bold;">&#40;</span>w<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#008000; font-style:italic;"># return [date, ..]</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;">days, year, month = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span> , <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">year</span>, <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">month</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">day = Date.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>year, month, <span style="color:#006666;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;"># first of this month</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;">matsu = Date.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>year, month, -<span style="color:#006666;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;"># last of this month</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;">until</span> day&gt; matsu <span style="color:#9966CC; font-weight:bold;">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;">days &lt;&lt;day.<span style="color:#9900CC;">dup</span> <span style="color:#9966CC; font-weight:bold;">if</span> day.<span style="color:#9900CC;">wday</span> == w</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">day += <span style="color:#006600; font-weight:bold;">&#40;</span>days.<span style="color:#9900CC;">length</span>&gt; <span style="color:#006666;color:#800000;">0</span> ? <span style="color:#006666;color:#800000;">7</span> : <span style="color:#006666;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#008000; font-style:italic;">#day</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;">days</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;"><span style="color:#CC0066; font-weight:bold;">p</span> <span style="color:#006600; font-weight:bold;">&#40;</span>Date.<span style="color:#9900CC;">today</span> + <span style="color:#006666;color:#800000;">6</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">wdays</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;color:#800000;">4</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;color:#800000;">2</span>,<span style="color:#006666;color:#800000;">4</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">map</span><span style="color:#006600; font-weight:bold;">&#123;</span>|d| d.<span style="color:#9900CC;">to_s</span><span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#9966CC; font-weight:bold;">if</span> $<span style="color:#006666;color:#800000;">0</span> == <span style="color:#0000FF; font-weight:bold;">__FILE__</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<h3>3.使い方</h3>
<p>Dateクラスに、下記の8つのメソッドが追加されます。</p>
<pre>weekdays(w) mondays tuesdays wednesdays thursdays fridays saturdays sundays</pre>
<p>weekdays(w)は、引数に曜日（月曜日を1とする）を取り、自身の年月の中から対象の曜日を配列で返します。<br />
それ以外は曜日名のメソッドで対応する曜日の配列を返します。</p>
<p>現在、2011年8月7日（日曜日）のため、『今日が「毎月第2、第4土曜日の6日前」かどうかを判定したい』と言う条件に変更してテストを実施します。</p>
<div class="igBar"><span id="lruby-38"><a href="#" onclick="javascript:showPlainTxt('ruby-38'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">RUBY:</span>
<div id="ruby-38">
<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;">d = Date.<span style="color:#9900CC;">today</span>+<span style="color:#006666;color:#800000;">6</span> <span style="color:#008000; font-style:italic;">#=&gt; #</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">d.<span style="color:#9900CC;">to_s</span> <span style="color:#008000; font-style:italic;">#(確認)=&gt; &quot;2011-08-13&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;">d.<span style="color:#9900CC;">saturdays</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;color:#800000;">1</span>,<span style="color:#006666;color:#800000;">3</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">map</span><span style="color:#006600; font-weight:bold;">&#123;</span>|w|w.<span style="color:#9900CC;">to_s</span><span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#008000; font-style:italic;">#(確認)=&gt; [&quot;2011-08-13&quot;, &quot;2011-08-27&quot;]</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">d.<span style="color:#9900CC;">saturdays</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;color:#800000;">1</span>,<span style="color:#006666;color:#800000;">3</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9966CC; font-weight:bold;">include</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>d<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;">#=&gt; true </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>うまくできました。<br />
これで日付の判定→通知を行う処理を1日1回cronで行えば目的が果たせそうです。</p>
]]></content:encoded>
			<wfw:commentRss>http://freestyle.nvo.jp/archives/891/feed</wfw:commentRss>
		</item>
		<item>
		<title>オープンソースグループウェア『aipo』について</title>
		<link>http://freestyle.nvo.jp/archives/857</link>
		<comments>http://freestyle.nvo.jp/archives/857#comments</comments>
		<pubDate>Fri, 05 Aug 2011 11:16:52 +0000</pubDate>
		<dc:creator>C.N</dc:creator>
		
		<category><![CDATA[技術情報]]></category>

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

		<guid isPermaLink="false">http://freestyle.nvo.jp/?p=857</guid>
		<description><![CDATA[オープンソースのグループウェア『aipo』について紹介したいと思います。
aipoは無料提供されているオープンソースのグループウェアです。
windows版とLinux版、クラウド版があり、windows版とLinux版 [...]]]></description>
			<content:encoded><![CDATA[<p>オープンソースのグループウェア『aipo』について紹介したいと思います。</p>
<p>aipoは無料提供されているオープンソースのグループウェアです。<br />
windows版とLinux版、クラウド版があり、windows版とLinux版は<br />
ダウンロードしインストールすると、すぐに試してみることができます。</p>
<p>公式サイト：<a href="http://www.aipo.com/">http://www.aipo.com/</a><br />
ダウンロード： <a href="http://free.aipo.com/download/">http://free.aipo.com/download/</a></p>
<p>インストール媒体にはTomcatやPostgreSQLなどの環境が含まれ、<br />
サンプルユーザ、サンプルデータが登録されているので、すぐに使うことができます。</p>
<p>スケジュールやToDo、アカウント管理など、基本的な機能はしっかり実装され、<br />
十分実運用に耐えられるのではないかと思います。</p>
<p><span id="more-857"></span>最新バージョンではアプリも追加することができ、Topページに美人時計を表示する<br />
などもカンタンにできます。<br />
また、回覧板などの無料のポートレット（機能）が<a href="http://aipo.systemfriend.co.jp/">Aipo工房</a>から提供されているので、こちらを追加することもできます。<br />
独自のポートレットを作成し、追加するなどのカスタマイズも可能です。</p>
<p>ただ、このポートレットを追加する際やカスタマイズする場合には、<br />
aipoのソースコードからのビルドが必要となってきます。</p>
<p>aipoはJetSpeed、Turbine、Velocity、CayenneといったApacheプロジェクトのフレームワークを基盤として設計されているため、こちらのフレームワークをある程度知っている必要があります。</p>
<p>以下のサイトをぜひ参考にしてください。</p>
<p>JetSpeed参考サイト：<br />
<a href="http://www.visards.co.jp/java/jetspeed/jetspeed03.html">http://www.visards.co.jp/java/jetspeed/jetspeed03.html</a></p>
<p>カスタマイズする際の参考URL：<br />
⇒開発者コミュニティ※現在のバージョンのaipo6用サイト<br />
<a href="http://code.google.com/p/aipo/wiki/GettingStandard">http://code.google.com/p/aipo/wiki/GettingStandard</a><br />
⇒旧開発者コミュニティ※aipo5用サイト<br />
<a href="http://user.aipo.com/develop/building/">http://user.aipo.com/develop/building/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://freestyle.nvo.jp/archives/857/feed</wfw:commentRss>
		</item>
	</channel>
</rss>

