<?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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
		xmlns:xhtml="http://www.w3.org/1999/xhtml"
>

<channel>
	<title>**deadwinter**</title>
	<atom:link href="http://blog.udzura.jp/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.udzura.jp</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Mon, 08 Mar 2010 07:40:31 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://blog.udzura.jp/feed/" />
		<item>
		<title>Object.const_getをハックした話</title>
		<link>http://blog.udzura.jp/2010/03/08/petit-hacking-about-const_get/</link>
		<comments>http://blog.udzura.jp/2010/03/08/petit-hacking-about-const_get/#comments</comments>
		<pubDate>Mon, 08 Mar 2010 07:40:31 +0000</pubDate>
		<dc:creator>udzura</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://blog.udzura.jp/?p=327</guid>
		<description><![CDATA[大変厳しい監視にも負けず、普通にRubyの話をします。
文字列のクラス名をクラスにするには、Rubyの場合evalとかもあるけれど（参照: Class.forNameはどう書くのか、コメントも）、 Object.const_get を使うのが一番安全な気がします。変なコードをねじ込まれてもNameErrorになるだけですし。

irb&#40;main&#41;:001:0&#62; Object.const_get&#40;&#34;String&#34;&#41;
#=&#62; String
irb&#40;main&#41;:002:0&#62; Object.const_get&#40;&#34;String&#34;&#41;.new
#=&#62; &#34;&#34;

でもこれ、たとえば Net::HTTP みたいな名前空間付きのクラスを呼ぶとエラーになりますんよ。

irb&#40;main&#41;:001:0&#62; require 'net/http'
=&#62; true
irb&#40;main&#41;:002:0&#62; Object.const_get&#40;&#34;Net::HTTP&#34;&#41;
NameError: wrong constant name Net::HTTP
	from &#40;irb&#41;:2:in `const_get'
	from (irb):2

なので、以下のようなメソッドをつくりました。
nested_const_get.rb

1
2
3
4
5
6
7
8
9
10
class Object
  def self.nested_const_get&#40;*args&#41;
    stack = &#40;args&#91;0&#93; =~ /::/&#41; ? args&#91;0&#93;.split&#40;&#34;::&#34;&#41; : args
    klass = Object
    while const = stack.shift
      [...]]]></description>
			<content:encoded><![CDATA[<p>大変厳しい監視にも負けず、普通にRubyの話をします。</p>
<p>文字列のクラス名をクラスにするには、Rubyの場合evalとかもあるけれど（参照: <a href="http://d.hatena.ne.jp/sai-ou89/20080518">Class.forNameはどう書くのか</a>、コメントも）、 <code>Object.const_get</code> を使うのが一番安全な気がします。変なコードをねじ込まれてもNameErrorになるだけですし。</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">irb<span style="color:#006600; font-weight:bold;">&#40;</span>main<span style="color:#006600; font-weight:bold;">&#41;</span>:001:<span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#CC00FF; font-weight:bold;">Object</span>.<span style="color:#9900CC;">const_get</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;String&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#008000; font-style:italic;">#=&gt; String</span>
irb<span style="color:#006600; font-weight:bold;">&#40;</span>main<span style="color:#006600; font-weight:bold;">&#41;</span>:002:<span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#CC00FF; font-weight:bold;">Object</span>.<span style="color:#9900CC;">const_get</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;String&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">new</span>
<span style="color:#008000; font-style:italic;">#=&gt; &quot;&quot;</span></pre></div></div>

<p>でもこれ、たとえば <code>Net::HTTP</code> みたいな名前空間付きのクラスを呼ぶとエラーになりますんよ。</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">irb<span style="color:#006600; font-weight:bold;">&#40;</span>main<span style="color:#006600; font-weight:bold;">&#41;</span>:001:<span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'net/http'</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>
irb<span style="color:#006600; font-weight:bold;">&#40;</span>main<span style="color:#006600; font-weight:bold;">&#41;</span>:002:<span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#CC00FF; font-weight:bold;">Object</span>.<span style="color:#9900CC;">const_get</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Net::HTTP&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC00FF; font-weight:bold;">NameError</span>: wrong constant name <span style="color:#6666ff; font-weight:bold;">Net::HTTP</span>
	from <span style="color:#006600; font-weight:bold;">&#40;</span>irb<span style="color:#006600; font-weight:bold;">&#41;</span>:<span style="color:#006666;">2</span>:<span style="color:#9966CC; font-weight:bold;">in</span> <span style="color:#996600;">`const_get'
	from (irb):2</span></pre></div></div>

<p>なので、以下のようなメソッドをつくりました。</p>
<p><strong><code>nested_const_get.rb</code></strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC00FF; font-weight:bold;">Object</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">nested_const_get</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>args<span style="color:#006600; font-weight:bold;">&#41;</span>
    stack = <span style="color:#006600; font-weight:bold;">&#40;</span>args<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span> =~ <span style="color:#006600; font-weight:bold;">/</span>::<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#41;</span> ? args<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;::&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> : args
    klass = <span style="color:#CC00FF; font-weight:bold;">Object</span>
    <span style="color:#9966CC; font-weight:bold;">while</span> const = stack.<span style="color:#9900CC;">shift</span>
      klass = klass.<span style="color:#9900CC;">const_get</span><span style="color:#006600; font-weight:bold;">&#40;</span>const<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    klass
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">irb<span style="color:#006600; font-weight:bold;">&#40;</span>main<span style="color:#006600; font-weight:bold;">&#41;</span>:001:<span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'nested_const_get'</span>
<span style="color:#008000; font-style:italic;">#=&gt; true</span>
irb<span style="color:#006600; font-weight:bold;">&#40;</span>main<span style="color:#006600; font-weight:bold;">&#41;</span>:002:<span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#CC00FF; font-weight:bold;">Object</span>.<span style="color:#9900CC;">nested_const_get</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;String&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#008000; font-style:italic;">#=&gt; String</span>
irb<span style="color:#006600; font-weight:bold;">&#40;</span>main<span style="color:#006600; font-weight:bold;">&#41;</span>:003:<span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'net/http'</span>
<span style="color:#008000; font-style:italic;">#=&gt; true</span>
irb<span style="color:#006600; font-weight:bold;">&#40;</span>main<span style="color:#006600; font-weight:bold;">&#41;</span>:004:<span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#CC00FF; font-weight:bold;">Object</span>.<span style="color:#9900CC;">nested_const_get</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Net::HTTP&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#008000; font-style:italic;">#=&gt; Net::HTTP</span>
irb<span style="color:#006600; font-weight:bold;">&#40;</span>main<span style="color:#006600; font-weight:bold;">&#41;</span>:005:<span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#CC00FF; font-weight:bold;">Object</span>.<span style="color:#9900CC;">nested_const_get</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Net::HTTP&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">start</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;blog.udzura.jp&quot;</span>, <span style="color:#006666;">80</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#008000; font-style:italic;">#=&gt; #&lt;Net::HTTP blog.udzura.jp:80 open=true&gt;</span>
irb<span style="color:#006600; font-weight:bold;">&#40;</span>main<span style="color:#006600; font-weight:bold;">&#41;</span>:006:<span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#CC00FF; font-weight:bold;">Object</span>.<span style="color:#9900CC;">nested_const_get</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Net::HTTP::Get&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#008000; font-style:italic;">#=&gt; Net::HTTP::Get</span>
irb<span style="color:#006600; font-weight:bold;">&#40;</span>main<span style="color:#006600; font-weight:bold;">&#41;</span>:007:<span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#CC00FF; font-weight:bold;">Object</span>.<span style="color:#9900CC;">nested_const_get</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Net::HTTP::Get::METHOD&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#008000; font-style:italic;">#=&gt; &quot;GET&quot;</span></pre></div></div>

<p>ハックというほどでもないですね。一応、何かの役に立つこともあろうかと。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.udzura.jp/2010/03/08/petit-hacking-about-const_get/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://blog.udzura.jp/2010/03/08/petit-hacking-about-const_get/" />
	</item>
		<item>
		<title>最近作ったもの@03/07</title>
		<link>http://blog.udzura.jp/2010/03/07/recent-cook-0306_0307/</link>
		<comments>http://blog.udzura.jp/2010/03/07/recent-cook-0306_0307/#comments</comments>
		<pubDate>Sun, 07 Mar 2010 12:58:29 +0000</pubDate>
		<dc:creator>udzura</dc:creator>
				<category><![CDATA[料理記録]]></category>

		<guid isPermaLink="false">http://blog.udzura.jp/?p=325</guid>
		<description><![CDATA[totteさんの厳しい監視にも負けず元気です。
某おいちゃんさんがほぼ毎日更新してて凄いので，僕も少しはブログ頑張ります。
3月6日


豚肉，ニラ，白菜などの炒め煮

西友（生命線）で豚の千切りとニラを味付けしたものが安かったので作った。


ポテトサラダ
ほうれん草のおひたし

初めて作った。
ほうれん草茹でる→冷やす→絞る→切る→200ccのだし汁に大さじ1の醤油



3月7日


カレー

新ジャガイモ　　1個
ニンジン　　1/3本
タマネギ　　1/2個
タケノコ水煮　　1/2個
豚ロース　　200g 四角く切る
水　　400cc
カレー粉　　1/4＋α箱分
気を遣ったことは，とにかく材料をあらかじめ炒める。
チーズ入れたら美味しかった。
ジャワカレーじゃなくてゴールデンカレー辛口が安かったので使った。普通にこの辛さなら好きなので問題ない


赤だし（インスタント）


＊　＊　＊
週末は料理作る習慣がついたので，たまにうpしますかね。
]]></description>
			<content:encoded><![CDATA[<p>totteさんの厳しい監視にも負けず元気です。</p>
<p>某おいちゃんさんがほぼ毎日更新してて凄いので，僕も少しはブログ頑張ります。</p>
<h3>3月6日</h3>
<p><a href="http://blog.udzura.jp/wp-content/uploads/2010/03/m_201003061919504b922c466bb3b.jpeg"><img src="http://blog.udzura.jp/wp-content/uploads/2010/03/m_201003061919504b922c466bb3b-300x225.jpg" alt="m_201003061919504b922c466bb3b" title="m_201003061919504b922c466bb3b" width="300" height="225" class="alignnone size-medium wp-image-323" /></a></p>
<ul>
<li>豚肉，ニラ，白菜などの炒め煮
<ul>
<li>西友（生命線）で豚の千切りとニラを味付けしたものが安かったので作った。</li>
</ul>
</li>
<li>ポテトサラダ</li>
<li>ほうれん草のおひたし
<ul>
<li>初めて作った。</li>
<li>ほうれん草茹でる→冷やす→絞る→切る→200ccのだし汁に大さじ1の醤油</li>
</ul>
</li>
</ul>
<h3>3月7日</h3>
<p><a href="http://blog.udzura.jp/wp-content/uploads/2010/03/m_201003072103024b9395f6693c2.jpeg"><img src="http://blog.udzura.jp/wp-content/uploads/2010/03/m_201003072103024b9395f6693c2-300x225.jpg" alt="m_201003072103024b9395f6693c2" title="m_201003072103024b9395f6693c2" width="300" height="225" class="alignnone size-medium wp-image-324" /></a></p>
<ul>
<li>カレー
<ul>
<li>新ジャガイモ　　1個</li>
<li>ニンジン　　1/3本</li>
<li>タマネギ　　1/2個</li>
<li>タケノコ水煮　　1/2個</li>
<li>豚ロース　　200g 四角く切る</li>
<li>水　　400cc</li>
<li>カレー粉　　1/4＋α箱分</li>
<li>気を遣ったことは，とにかく材料をあらかじめ炒める。</li>
<li>チーズ入れたら美味しかった。</li>
<li>ジャワカレーじゃなくてゴールデンカレー辛口が安かったので使った。普通にこの辛さなら好きなので問題ない</li>
</ul>
</li>
<li>赤だし（インスタント）
</li>
</ul>
<h3>＊　＊　＊</h3>
<p>週末は料理作る習慣がついたので，たまにうpしますかね。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.udzura.jp/2010/03/07/recent-cook-0306_0307/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://blog.udzura.jp/2010/03/07/recent-cook-0306_0307/" />
	</item>
		<item>
		<title>近況</title>
		<link>http://blog.udzura.jp/2010/03/06/recent/</link>
		<comments>http://blog.udzura.jp/2010/03/06/recent/#comments</comments>
		<pubDate>Sat, 06 Mar 2010 02:18:17 +0000</pubDate>
		<dc:creator>udzura</dc:creator>
				<category><![CDATA[＜雑＞]]></category>

		<guid isPermaLink="false">http://blog.udzura.jp/?p=320</guid>
		<description><![CDATA[生きてますし、ブログも更新したい気持ちはあります。
最近は，触ったことのないSQL ServerにRuby接続するのを一日で覚えたり，触ったことのないCapstranoのレシピをWebistranoに移行するとかそういう感じです。
もう少し落ち着いたら何か書きます。
]]></description>
			<content:encoded><![CDATA[<p>生きてますし、ブログも更新したい気持ちはあります。</p>
<p>最近は，触ったことのないSQL ServerにRuby接続するのを一日で覚えたり，触ったことのないCapstranoのレシピをWebistranoに移行するとかそういう感じです。</p>
<p>もう少し落ち着いたら何か書きます。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.udzura.jp/2010/03/06/recent/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://blog.udzura.jp/2010/03/06/recent/" />
	</item>
		<item>
		<title>mooolsがアルバム</title>
		<link>http://blog.udzura.jp/2010/02/27/moools-album-releas/</link>
		<comments>http://blog.udzura.jp/2010/02/27/moools-album-releas/#comments</comments>
		<pubDate>Sat, 27 Feb 2010 06:34:19 +0000</pubDate>
		<dc:creator>udzura</dc:creator>
				<category><![CDATA[release]]></category>

		<guid isPermaLink="false">http://blog.udzura.jp/?p=315</guid>
		<description><![CDATA[アルバム出してた。とっくに入手可能じゃないか…
http://www.moools.com/moools_news_01.html
店頭流通は，3月10日からだそうです。
http://www.myspace.com/moools
で，さっそくMySpaceで試聴してみましたが，このバンドはものすごくスルメなので初回聴きでは真価が分かりかねるところが。。。「アドバルーン」みたいな曲が好きです。
OTOTOYでは全曲試聴できる模様ですね。まずはゆっくり聴いてみましょうか。
＊　＊　＊
で、聴いてみました。変拍子ブログなので，注目すべきはTr.10「気象衛星」ですね。mooolsがここまで普通に変拍子入ってるのって実は初めてなんじゃ。。
変拍子クラスタじゃない方は Tr.3、6～8の流れとかがいい感じなんじゃないでしょうか。ていうか普通に良いアルバムですね！　僕はタワレコのポイントのために（w　3月10日まで待つ予定ですが，入手が楽しみです。
]]></description>
			<content:encoded><![CDATA[<p>アルバム出してた。とっくに入手可能じゃないか…</p>
<p><a href="http://www.moools.com/moools_news_01.html">http://www.moools.com/moools_news_01.html</a></p>
<p>店頭流通は，3月10日からだそうです。</p>
<p><a href="http://www.myspace.com/moools">http://www.myspace.com/moools</a></p>
<p>で，さっそくMySpaceで試聴してみましたが，このバンドはものすごくスルメなので初回聴きでは真価が分かりかねるところが。。。「アドバルーン」みたいな曲が好きです。</p>
<p><a href="http://ototoy.jp/pkg/Weather_Sketch_Modified-moools/cd/12010">OTOTOY</a>では全曲試聴できる模様ですね。まずはゆっくり聴いてみましょうか。</p>
<h3>＊　＊　＊</h3>
<p>で、聴いてみました。変拍子ブログなので，注目すべきはTr.10「気象衛星」ですね。mooolsがここまで普通に変拍子入ってるのって実は初めてなんじゃ。。</p>
<p>変拍子クラスタじゃない方は Tr.3、6～8の流れとかがいい感じなんじゃないでしょうか。ていうか普通に良いアルバムですね！　僕はタワレコのポイントのために（w　3月10日まで待つ予定ですが，入手が楽しみです。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.udzura.jp/2010/02/27/moools-album-releas/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://blog.udzura.jp/2010/02/27/moools-album-releas/" />
	</item>
		<item>
		<title>経過報告（某ライブハウスのスケジュールをパース）</title>
		<link>http://blog.udzura.jp/2010/02/21/scraping-o-group-schedules/</link>
		<comments>http://blog.udzura.jp/2010/02/21/scraping-o-group-schedules/#comments</comments>
		<pubDate>Sun, 21 Feb 2010 06:16:33 +0000</pubDate>
		<dc:creator>udzura</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[＜音楽＞]]></category>

		<guid isPermaLink="false">http://blog.udzura.jp/?p=311</guid>
		<description><![CDATA[休みなので，何かを作っています。
その中間生産物として，以下のようなスクリプトを作りました。

1
./o_summary.rb

ってやると，2010年3月（とりあえず決め打ち）のO-GROUPのライブ予定をまとめて表示してくれます。

udzura@ubuntu-vaio:~/dev$ ./o_watch_test.rb
＠＠＠O-East＠＠＠
●2010年3月01日 ＠ O-East
□テリアの東 「空のエチュード　II」
Dichten
ソノダバンド／WADAIKO龍鼓会／COKEBOTTLE
#dicline／LIVING IN THE DARK
ゲスト:C’est La Vie
開場17:30／開演18:00●前売3000／当日3500●ドリンク別
問合せ：03-5458-4681 O-EAST
Dichten
=> http://shibuya-o.com/category/east/?id=schedule&#038;Year=2010&#038;Month=3#post-9701

----
●2010年3月02日 ＠ O-East
□PUNK ROCK CONFIDENTIAL JAPAN presents PUNKAFOOLIC! KOTTONMOUTH KINGS JAPAN TOUR
KOTTONMOUTH KINGS
開場19:00／開演20:00●前売5000／当日未定●ドリンク別
発売中●ぴあ(344-442)・ローソン(70471)・e+
※未就学児童入場不可
問合せ：03-3475-9999　H.I.P
http://www.punkafoolic.com/

=> http://shibuya-o.com/category/east/?id=schedule&#038;Year=2010&#038;Month=3#post-8977

----
●2010年3月03日 ＠ O-East
□美神降臨〜LIV MOON FIRST CLUB SHOW
LIV MOON
開場18:00／開演19:00●前売5500／当日未定●ドリンク別
発売中●ぴあ(345-509)・ローソン(74305)・e+・CN
問合せ：03-3462-6969　クリエイティブマン

=> http://shibuya-o.com/category/east/?id=schedule&#038;Year=2010&#038;Month=3#post-9126

----
●2010年3月04日 ＠ O-East
□たむらぱん　パンタスティックツアー
たむらぱん
開場19:00／開演19:30●前売4500／当日5000●ドリンク別
発売中●ぴあ(342-160)・ローソン(78625)・e+・CN
問合せ：03-3498-9999キョードー東京

=> http://shibuya-o.com/category/east/?id=schedule&#038;Year=2010&#038;Month=3#post-8702

----
●2010年3月05日 ＠ O-East
□おわらないせかいのうたいかた
……


Hpricot ライブラリを使用。
（本当はAPIのデザイン的にnokogiriを使いたかったのだが，文字化けする。。。　多分，ヘッダのmeta情報だけなぜかS_JISだからだと思うんだけど，困った）
■ o_summary.rb

#!/usr/bin/ruby
# o-group データ自動取得
&#160;
require 'rubygems'
require 'hpricot'
require 'open-uri'
&#160;
$KCODE = 'u'
&#160;
houses = &#91;&#34;East&#34;, &#34;West&#34;, &#34;Nest&#34;, &#34;Crest&#34;&#93;
&#160;
y = 2010
m = [...]]]></description>
			<content:encoded><![CDATA[<p>休みなので，何かを作っています。</p>
<p>その中間生産物として，以下のようなスクリプトを作りました。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>o_summary.rb</pre></td></tr></table></div>

<p>ってやると，2010年3月（とりあえず決め打ち）の<a href="http://www.shibuya-o.com/">O-GROUP</a>のライブ予定をまとめて表示してくれます。</p>
<pre>
udzura@ubuntu-vaio:~/dev$ ./o_watch_test.rb
＠＠＠O-East＠＠＠
●2010年3月01日 ＠ O-East
□テリアの東 「空のエチュード　II」
Dichten
ソノダバンド／WADAIKO龍鼓会／COKEBOTTLE
#dicline／LIVING IN THE DARK
ゲスト:C’est La Vie
開場17:30／開演18:00●前売3000／当日3500●ドリンク別
問合せ：03-5458-4681 O-EAST
Dichten
=> http://shibuya-o.com/category/east/?id=schedule&#038;Year=2010&#038;Month=3#post-9701

----
●2010年3月02日 ＠ O-East
□PUNK ROCK CONFIDENTIAL JAPAN presents PUNKAFOOLIC! KOTTONMOUTH KINGS JAPAN TOUR
KOTTONMOUTH KINGS
開場19:00／開演20:00●前売5000／当日未定●ドリンク別
発売中●ぴあ(344-442)・ローソン(70471)・e+
※未就学児童入場不可
問合せ：03-3475-9999　H.I.P
http://www.punkafoolic.com/

=> http://shibuya-o.com/category/east/?id=schedule&#038;Year=2010&#038;Month=3#post-8977

----
●2010年3月03日 ＠ O-East
□美神降臨〜LIV MOON FIRST CLUB SHOW
LIV MOON
開場18:00／開演19:00●前売5500／当日未定●ドリンク別
発売中●ぴあ(345-509)・ローソン(74305)・e+・CN
問合せ：03-3462-6969　クリエイティブマン

=> http://shibuya-o.com/category/east/?id=schedule&#038;Year=2010&#038;Month=3#post-9126

----
●2010年3月04日 ＠ O-East
□たむらぱん　パンタスティックツアー
たむらぱん
開場19:00／開演19:30●前売4500／当日5000●ドリンク別
発売中●ぴあ(342-160)・ローソン(78625)・e+・CN
問合せ：03-3498-9999キョードー東京

=> http://shibuya-o.com/category/east/?id=schedule&#038;Year=2010&#038;Month=3#post-8702

----
●2010年3月05日 ＠ O-East
□おわらないせかいのうたいかた
……
</pre>
<p><span id="more-311"></span><br />
<a href="http://github.com/hpricot/hpricot">Hpricot</a> ライブラリを使用。</p>
<p>（本当はAPIのデザイン的に<a href="http://nokogiri.org/">nokogiri</a>を使いたかったのだが，文字化けする。。。　多分，ヘッダのmeta情報だけなぜかS_JISだからだと思うんだけど，困った）</p>
<p>■ <code><strong>o_summary.rb</strong></code></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#!/usr/bin/ruby</span>
<span style="color:#008000; font-style:italic;"># o-group データ自動取得</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rubygems'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'hpricot'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'open-uri'</span>
&nbsp;
<span style="color:#ff6633; font-weight:bold;">$KCODE</span> = <span style="color:#996600;">'u'</span>
&nbsp;
houses = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;East&quot;</span>, <span style="color:#996600;">&quot;West&quot;</span>, <span style="color:#996600;">&quot;Nest&quot;</span>, <span style="color:#996600;">&quot;Crest&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
y = <span style="color:#006666;">2010</span>
m = <span style="color:#006666;">3</span>
&nbsp;
houses.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>house<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;＠＠＠O-#{house}＠＠＠&quot;</span>
  url = <span style="color:#996600;">&quot;http://shibuya-o.com/category/#{house.downcase}/?id=schedule&amp;Year=#{y}&amp;Month=#{m}&quot;</span>
  doc = Hpricot<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span>url<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  date = <span style="color:#006600; font-weight:bold;">&#40;</span>doc<span style="color:#006600; font-weight:bold;">/</span><span style="color:#996600;">&quot;.scheduledate&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  date.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>elm<span style="color:#006600; font-weight:bold;">|</span>
    d = elm.<span style="color:#9900CC;">inner_text</span>.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">match</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#008000; font-style:italic;">#{&quot;%02d&quot; % m}\/(\d\d)/)[1]</span>
    title = <span style="color:#006600; font-weight:bold;">&#40;</span>elm<span style="color:#006600; font-weight:bold;">/</span><span style="color:#996600;">&quot;../../../td/span[@class='scheduletitle']&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">inner_text</span>.<span style="color:#9900CC;">strip</span>
    desc = <span style="color:#006600; font-weight:bold;">&#40;</span>elm<span style="color:#006600; font-weight:bold;">/</span><span style="color:#996600;">&quot;../../../td/p&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#91;</span>1..4<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">map</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>v<span style="color:#006600; font-weight:bold;">|</span> v.<span style="color:#9900CC;">inner_text</span>.<span style="color:#9900CC;">strip</span><span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;<span style="color:#000099;">\n</span>&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    post_id = <span style="color:#006600; font-weight:bold;">&#40;</span>elm<span style="color:#006600; font-weight:bold;">/</span><span style="color:#996600;">&quot;../../../td/span[@class='scheduletitle']&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;id&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    permlink = <span style="color:#996600;">&quot;#{url}##{post_id}&quot;</span>
    <span style="color:#CC0066; font-weight:bold;">print</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;-</span><span style="color:#996600;">&quot;EOD&quot;</span>
●<span style="color:#008000; font-style:italic;">#{y}年#{m}月#{d}日 ＠ O-#{house}</span>
□<span style="color:#008000; font-style:italic;">#{title}</span>
<span style="color:#008000; font-style:italic;">#{desc}</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#008000; font-style:italic;">#{permlink}</span>
&nbsp;
<span style="color:#006600; font-weight:bold;">----</span>
    EOD
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">exit</span> <span style="color:#006666;">0</span></pre></div></div>

<p>書いてみて思ったんだけど，本当に短いコードでやりたいことができた。ビックリだわ。<br />
このコードを元にフヒヒするよてい。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.udzura.jp/2010/02/21/scraping-o-group-schedules/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://blog.udzura.jp/2010/02/21/scraping-o-group-schedules/" />
	</item>
		<item>
		<title>Rubyで色を判断（適当バージョン）</title>
		<link>http://blog.udzura.jp/2010/02/16/detect-color-with-ruby_random-way/</link>
		<comments>http://blog.udzura.jp/2010/02/16/detect-color-with-ruby_random-way/#comments</comments>
		<pubDate>Tue, 16 Feb 2010 02:48:06 +0000</pubDate>
		<dc:creator>udzura</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://blog.udzura.jp/?p=309</guid>
		<description><![CDATA[色コード「#RRGGBB」をパースして，適当でいいので何色か判断するクラスを作りたい。ちょっとしたアレで使用したいので。
最初に思いついたもの
やっつけなのでろくにテストも書いていない。。

rgbcolor.rb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
class RGBColor
  def initialize&#40;r, g, b&#41;
    raise ArgumentError unless &#91;r, g, b&#93;.all?&#123;&#124;v&#124; &#40;0..255&#41; === v&#125;
    @r = r
    @g = g
    @b = b
    @color_type = detect_color
  end
  attr_accessor :r, :g, :b
&#160;
 [...]]]></description>
			<content:encoded><![CDATA[<p>色コード「#RRGGBB」をパースして，適当でいいので何色か判断するクラスを作りたい。ちょっとしたアレで使用したいので。</p>
<h3>最初に思いついたもの</h3>
<p>やっつけなのでろくにテストも書いていない。。</p>
<p><span id="more-309"></span></p>
<p><strong><code>rgbcolor.rb</code></strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#!/usr/bin/env ruby</span>
<span style="color:#008000; font-style:italic;"># -*- coding: utf-8 -*-</span>
<span style="color:#9966CC; font-weight:bold;">class</span> RGBColor
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>r, g, b<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#CC00FF; font-weight:bold;">ArgumentError</span> <span style="color:#9966CC; font-weight:bold;">unless</span> <span style="color:#006600; font-weight:bold;">&#91;</span>r, g, b<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">all</span>?<span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>v<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">&#40;</span>0..255<span style="color:#006600; font-weight:bold;">&#41;</span> === v<span style="color:#006600; font-weight:bold;">&#125;</span>
    <span style="color:#0066ff; font-weight:bold;">@r</span> = r
    <span style="color:#0066ff; font-weight:bold;">@g</span> = g
    <span style="color:#0066ff; font-weight:bold;">@b</span> = b
    <span style="color:#0066ff; font-weight:bold;">@color_type</span> = detect_color
  <span style="color:#9966CC; font-weight:bold;">end</span>
  attr_accessor <span style="color:#ff3333; font-weight:bold;">:r</span>, <span style="color:#ff3333; font-weight:bold;">:g</span>, <span style="color:#ff3333; font-weight:bold;">:b</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> inspect
    <span style="color:#996600;">&quot;#&lt;RGBColor(#{@r}, #{@g}, #{@b}), @color_type=#{@color_type}&gt;&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> detect_color
    cr = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#40;</span>@r <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_f</span> <span style="color:#006600; font-weight:bold;">/</span> <span style="color:#006600; font-weight:bold;">&#40;</span>@g <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006666;">1</span> <span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#006600; font-weight:bold;">&#40;</span>@g <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_f</span> <span style="color:#006600; font-weight:bold;">/</span> <span style="color:#006600; font-weight:bold;">&#40;</span>@b <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006666;">1</span> <span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#006600; font-weight:bold;">&#40;</span>@b <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_f</span> <span style="color:#006600; font-weight:bold;">/</span> <span style="color:#006600; font-weight:bold;">&#40;</span>@r <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006666;">1</span> <span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    <span style="color:#9966CC; font-weight:bold;">if</span> cr.<span style="color:#9900CC;">all</span>?<span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>v<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">&#40;</span>0.9..1.1<span style="color:#006600; font-weight:bold;">&#41;</span> === v<span style="color:#006600; font-weight:bold;">&#125;</span>
      <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@r</span> <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#006666;">52</span>
        color_type = <span style="color:#ff3333; font-weight:bold;">:black</span>
      <span style="color:#9966CC; font-weight:bold;">elsif</span> <span style="color:#0066ff; font-weight:bold;">@r</span> <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#006666;">188</span>
        color_type = <span style="color:#ff3333; font-weight:bold;">:gray</span>
      <span style="color:#9966CC; font-weight:bold;">else</span>
        color_type = <span style="color:#ff3333; font-weight:bold;">:white</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">elsif</span> cr<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&gt;</span> cr<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>
      <span style="color:#9966CC; font-weight:bold;">if</span> cr<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&gt;</span> cr<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#93;</span>
        color_type = <span style="color:#ff3333; font-weight:bold;">:red</span>
      <span style="color:#9966CC; font-weight:bold;">elsif</span> cr<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&gt;</span> cr<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#93;</span>
        color_type = <span style="color:#ff3333; font-weight:bold;">:purple</span>
      <span style="color:#9966CC; font-weight:bold;">else</span>
        color_type = <span style="color:#ff3333; font-weight:bold;">:blue</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">else</span>
      <span style="color:#9966CC; font-weight:bold;">if</span> cr<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&gt;</span> cr<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#93;</span>
        color_type = <span style="color:#ff3333; font-weight:bold;">:yellow</span>
      <span style="color:#9966CC; font-weight:bold;">elsif</span> cr<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&gt;</span> cr<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#93;</span>
        color_type = <span style="color:#ff3333; font-weight:bold;">:green</span>
      <span style="color:#9966CC; font-weight:bold;">else</span>
        color_type = <span style="color:#ff3333; font-weight:bold;">:cyan</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    color_type
  <span style="color:#9966CC; font-weight:bold;">end</span>
  private <span style="color:#ff3333; font-weight:bold;">:detect_color</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">parse_hex</span><span style="color:#006600; font-weight:bold;">&#40;</span>str<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#CC00FF; font-weight:bold;">ArgumentError</span> <span style="color:#9966CC; font-weight:bold;">unless</span> <span style="color:#006600; font-weight:bold;">/</span>^<span style="color:#008000; font-style:italic;">#?([a-fA-F0-9]{6})$/ =~ str</span>
    ccode = $1.<span style="color:#9900CC;">to_i</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">16</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    r = <span style="color:#006600; font-weight:bold;">&#40;</span>ccode <span style="color:#006600; font-weight:bold;">&gt;&gt;</span> <span style="color:#006666;">16</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&amp;</span> <span style="color:#006666;">255</span>
    g = <span style="color:#006600; font-weight:bold;">&#40;</span>ccode <span style="color:#006600; font-weight:bold;">&gt;&gt;</span> <span style="color:#006666;">8</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&amp;</span> <span style="color:#006666;">255</span>
    b = ccode <span style="color:#006600; font-weight:bold;">&amp;</span> <span style="color:#006666;">255</span>
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>r, g, b<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0000FF; font-weight:bold;">__FILE__</span> == $<span style="color:#006666;">0</span>
  <span style="color:#9966CC; font-weight:bold;">case</span> ARGV.<span style="color:#9900CC;">size</span>
  <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;">1</span>
    <span style="color:#CC0066; font-weight:bold;">p</span> RGBColor.<span style="color:#9900CC;">parse_hex</span><span style="color:#006600; font-weight:bold;">&#40;</span>ARGV<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;">3</span>
    <span style="color:#CC0066; font-weight:bold;">p</span> RGBColor.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>ARGV<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">to_i</span>, ARGV<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">to_i</span>, ARGV<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">to_i</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">else</span>
    STDERR.<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;usage: colorhex.rb (R G B|RRGGBB)&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">udzura@ubuntu<span style="color:#006600; font-weight:bold;">-</span>vaio:~<span style="color:#006600; font-weight:bold;">/</span>dev$ irb <span style="color:#006600; font-weight:bold;">-</span>rrgbcolor
irb<span style="color:#006600; font-weight:bold;">&#40;</span>main<span style="color:#006600; font-weight:bold;">&#41;</span>:001:<span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&gt;</span> RGBColor.<span style="color:#9900CC;">parse_hex</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#aa66cc&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#008000; font-style:italic;">#&lt;RGBColor(170, 102, 204), @color_type=:purple&gt;</span>
irb<span style="color:#006600; font-weight:bold;">&#40;</span>main<span style="color:#006600; font-weight:bold;">&#41;</span>:002:<span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&gt;</span> RGBColor.<span style="color:#9900CC;">parse_hex</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#dd0000&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#008000; font-style:italic;">#&lt;RGBColor(221, 0, 0), @color_type=:red&gt;</span>
irb<span style="color:#006600; font-weight:bold;">&#40;</span>main<span style="color:#006600; font-weight:bold;">&#41;</span>:003:<span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&gt;</span> RGBColor.<span style="color:#9900CC;">parse_hex</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#000000&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#008000; font-style:italic;">#&lt;RGBColor(0, 0, 0), @color_type=:black&gt;</span>
irb<span style="color:#006600; font-weight:bold;">&#40;</span>main<span style="color:#006600; font-weight:bold;">&#41;</span>:004:<span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&gt;</span> RGBColor.<span style="color:#9900CC;">parse_hex</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#888888&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#008000; font-style:italic;">#&lt;RGBColor(136, 136, 136), @color_type=:gray&gt;</span>
irb<span style="color:#006600; font-weight:bold;">&#40;</span>main<span style="color:#006600; font-weight:bold;">&#41;</span>:005:<span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&gt;</span> RGBColor.<span style="color:#9900CC;">parse_hex</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#00aa88&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#008000; font-style:italic;">#&lt;RGBColor(0, 170, 136), @color_type=:cyan&gt;</span>
irb<span style="color:#006600; font-weight:bold;">&#40;</span>main<span style="color:#006600; font-weight:bold;">&#41;</span>:006:<span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&gt;</span> RGBColor.<span style="color:#9900CC;">parse_hex</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#00aa44&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#008000; font-style:italic;">#&lt;RGBColor(0, 170, 68), @color_type=:cyan&gt;</span>
irb<span style="color:#006600; font-weight:bold;">&#40;</span>main<span style="color:#006600; font-weight:bold;">&#41;</span>:007:<span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&gt;</span> RGBColor.<span style="color:#9900CC;">parse_hex</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#00aa00&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#008000; font-style:italic;">#&lt;RGBColor(0, 170, 0), @color_type=:green&gt;</span></pre></td></tr></table></div>

<p>大体合ってなくはないけど，何かいいかげん。。。</p>
<h3>アルゴリズム</h3>
<p>色（r, g, b）（0≦r, g, b≦255）に対して，以下の条件が成り立つなら→で示す色であるとする。</p>
<pre>
r ＞ g ＝ b　→　赤
r ＜ g ＝ b　→　水色

g ＞ r ＝ b　→　緑
g ＜ r ＝ b　→　紫

b ＞ r ＝ g　→　青
b ＜ r ＝ g　→　黄色
</pre>
<p>以上を (A) とする。</p>
<p>また，以下を(1)，(2)，(3)とする。</p>
<pre>
 r + 1
-------　…　(1)
 g + 1

 g + 1
-------　…　(2)
 b + 1

 b + 1
-------　…　(3)
 r + 1
</pre>
<p>(A)と(1)，(2)，(3)から，以下が言える。</p>
<pre>
(1) ＞ (2) ＞ (3)　→　赤
(3) ＞ (2) ＞ (1)　→　水色

(2) ＞ (3) ＞ (1)　→　緑
(1) ＞ (3) ＞ (2)　→　紫

(3) ＞ (1) ＞ (2)　→　青
(2) ＞ (1) ＞ (3)　→　黄色
</pre>
<p>また，(1)，(2)，(3)すべてが1に近い場合は無彩色であろう。なので、rの値で黒，灰色，白を判断する。</p>
<h3>これはひどい車輪の再発明</h3>
<p>そもそもRubyには<a href="http://rmagick.rubyforge.org/">RMagick</a>と言う素晴らしいライブラリが用意されており，それは<a href="http://www.imagemagick.org/script/index.php">ImageMagick</a>というCUIの画像加工ツールのラッパーである（説明口調）。</p>
<p>色々調べたら，HSLという表現方法のほうが「結局お前何色よ？」が分かりやすくて，RMagickなら <code>Pixcel#to_hlsa</code> とかそういうメソッドで変換できるそうな。どう考えてもこっちの方がいいな……。</p>
<ul>
<li>参考1: <a href="http://ja.wikipedia.org/wiki/HLS%E8%89%B2%E7%A9%BA%E9%96%93">Wikipedia &#8211; HLS色空間</a></li>
<li>参考2: <a href="http://smash.nobuto-murata.org/2009/12/imagemagickhslhsv.html">ImageMagickでHSLとHSV色空間を理解する</a></li>
<li>参考3: <a href="http://www.imagemagick.org/RMagick/doc/struct.html#to_hsla">Pixcel#to_hlsa</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.udzura.jp/2010/02/16/detect-color-with-ruby_random-way/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://blog.udzura.jp/2010/02/16/detect-color-with-ruby_random-way/" />
	</item>
		<item>
		<title>emacs23始めました on Ubuntu 9.10</title>
		<link>http://blog.udzura.jp/2010/02/15/rails-settings-with-emacs23-on-ubuntu-910/</link>
		<comments>http://blog.udzura.jp/2010/02/15/rails-settings-with-emacs23-on-ubuntu-910/#comments</comments>
		<pubDate>Mon, 15 Feb 2010 11:17:41 +0000</pubDate>
		<dc:creator>udzura</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://blog.udzura.jp/?p=305</guid>
		<description><![CDATA[標記のとおり。遅まきながら，ですが。
普段，NetBeansのRailsモードで開発してたんですが，Rinariなる凄く便利なelispができたようなので，試すついでに色々設定してみました。emacsは23.1.1です。パッケージで言うとemacs23、と明示的に指定します。単純に23のほうが日本語フォントの設定がしやすいからです。。
インストールと設定
一部，emacsとRinariで快適Rails開発！ と言う素晴らしい記事を激しくコピペしつつ，以下の手順です。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
sudo aptitude install \
  emacs23 emacs23-common emacs23-bin-common emacs23-el \
  emacs-goodies-el \
  ruby1.8-elisp \
  yaml-mode
cd ~/.emacs.d
&#160;
# install rinari
git clone git://github.com/eschulte/rinari.git
cd rinari
git submodule init
git submodule update
cd -
&#160;
# install rhtml mode
git clone git://github.com/eschulte/rhtml.git

「emacs-goodies-el」と言うパッケージをいれています。テーマが色々選べるようになって快適です。emacs22、23どちらでも使えてます。配色のサンプルページ（重め）もあったりします。
手動で変えるときは M-x color-theme-select です。その日の気分で変えてもいいぐらい手軽に変更できます。
~/.emacs は、こちらもコピペをしまくった結果、以下の設定で満足しました。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
;; 言語設定とか
&#40;set-language-environment &#34;Japanese&#34;&#41;
&#40;set-terminal-coding-system 'utf-8&#41;
&#40;set-keyboard-coding-system 'utf-8&#41;
&#40;set-buffer-file-coding-system 'utf-8&#41;
&#40;setq default-buffer-file-coding-system 'utf-8&#41;
&#160;
;; フォント設定
;; ここが、emacs23じゃないとうまいこと行かなかった。。
&#40;set-face-attribute 'default nil
  :family &#34;VL Gothic&#34;
  [...]]]></description>
			<content:encoded><![CDATA[<p>標記のとおり。遅まきながら，ですが。</p>
<p>普段，NetBeansのRailsモードで開発してたんですが，<a href="http://rinari.rubyforge.org/">Rinari</a>なる凄く便利なelispができたようなので，試すついでに色々設定してみました。emacsは23.1.1です。パッケージで言うとemacs23、と明示的に指定します。単純に23のほうが日本語フォントの設定がしやすいからです。。</p>
<h3>インストールと設定</h3>
<p>一部，<a href="http://d.hatena.ne.jp/willnet/20090110/1231595231/">emacsとRinariで快適Rails開発！</a> と言う素晴らしい記事を激しくコピペしつつ，以下の手順です。</p>
<p><span id="more-305"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">aptitude</span> <span style="color: #c20cb9; font-weight: bold;">install</span> \
  emacs23 emacs23-common emacs23-bin-common emacs23-el \
  emacs-goodies-el \
  ruby1.8-elisp \
  yaml-mode
<span style="color: #7a0874; font-weight: bold;">cd</span> ~<span style="color: #000000; font-weight: bold;">/</span>.emacs.d
&nbsp;
<span style="color: #666666; font-style: italic;"># install rinari</span>
git clone git:<span style="color: #000000; font-weight: bold;">//</span>github.com<span style="color: #000000; font-weight: bold;">/</span>eschulte<span style="color: #000000; font-weight: bold;">/</span>rinari.git
<span style="color: #7a0874; font-weight: bold;">cd</span> rinari
git submodule init
git submodule update
<span style="color: #7a0874; font-weight: bold;">cd</span> -
&nbsp;
<span style="color: #666666; font-style: italic;"># install rhtml mode</span>
git clone git:<span style="color: #000000; font-weight: bold;">//</span>github.com<span style="color: #000000; font-weight: bold;">/</span>eschulte<span style="color: #000000; font-weight: bold;">/</span>rhtml.git</pre></td></tr></table></div>

<p>「emacs-goodies-el」と言うパッケージをいれています。テーマが色々選べるようになって快適です。emacs22、23どちらでも使えてます。<a href="http://code.google.com/p/gnuemacscolorthemetest/">配色のサンプルページ（重め）</a>もあったりします。</p>
<p>手動で変えるときは <code>M-x color-theme-select</code> です。その日の気分で変えてもいいぐらい手軽に変更できます。</p>
<p><code>~/.emacs</code> は、こちらもコピペをしまくった結果、以下の設定で満足しました。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
</pre></td><td class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">;; 言語設定とか</span>
<span style="color: #66cc66;">&#40;</span>set-language-environment <span style="color: #ff0000;">&quot;Japanese&quot;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>set-terminal-coding-system 'utf-<span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>set-keyboard-coding-system 'utf-<span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>set-buffer-file-coding-system 'utf-<span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setq</span> default-buffer-file-coding-system 'utf-<span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">;; フォント設定</span>
<span style="color: #808080; font-style: italic;">;; ここが、emacs23じゃないとうまいこと行かなかった。。</span>
<span style="color: #66cc66;">&#40;</span>set-face-attribute 'default <span style="color: #b1b100;">nil</span>
  <span style="color: #66cc66;">:</span><span style="color: #555;">family</span> <span style="color: #ff0000;">&quot;VL Gothic&quot;</span>
  <span style="color: #66cc66;">:</span><span style="color: #555;">height</span> <span style="color: #cc66cc;">130</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">;; 起動時のフレームサイズ</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setq</span> initial-frame-alist '<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>width <span style="color: #66cc66;">.</span> <span style="color: #cc66cc;">120</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>height <span style="color: #66cc66;">.</span> <span style="color: #cc66cc;">45</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">;; テーマ読み込み</span>
<span style="color: #66cc66;">&#40;</span>require 'color-theme<span style="color: #66cc66;">&#41;</span>
<span style="color: #808080; font-style: italic;">;; ここでテーマ設定、好きなものに変更</span>
<span style="color: #66cc66;">&#40;</span>color-theme-hober<span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">;; Interactively Do Things (highly recommended, but not strictly required)</span>
<span style="color: #66cc66;">&#40;</span>require 'ido<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>ido-mode t<span style="color: #66cc66;">&#41;</span>
<span style="color: #808080; font-style: italic;">;; Rinari</span>
<span style="color: #66cc66;">&#40;</span>add-to-<span style="color: #b1b100;">list</span> 'load-path <span style="color: #ff0000;">&quot;~/.emacs.d/rinari&quot;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>require 'rinari<span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">;; rhtml-mode</span>
<span style="color: #66cc66;">&#40;</span>add-to-<span style="color: #b1b100;">list</span> 'load-path <span style="color: #ff0000;">&quot;~/.emacs.d/rhtml&quot;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>require 'rhtml-mode<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>add-hook 'rhtml-mode-hook
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>rinari-launch<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">;; 何か最後がいいらしい</span>
<span style="color: #66cc66;">&#40;</span>set-default-coding-systems 'utf-<span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>で，なんでまたこんなエントリをぶち上げたかといいますと，近いうちに開発環境をセットアップする必要があり，その時のためのメモです（w</p>
<h3>追記＠2010-03-05</h3>
<p>最新の.emacsファイルは以下にアップすることにしました。</p>
<p>＊ <a href="http://github.com/udzura/my-emacs/blob/master/.emacs">http://github.com/udzura/my-emacs/blob/master/.emacs</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.udzura.jp/2010/02/15/rails-settings-with-emacs23-on-ubuntu-910/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://blog.udzura.jp/2010/02/15/rails-settings-with-emacs23-on-ubuntu-910/" />
	</item>
		<item>
		<title>grub2のちょっと良い話</title>
		<link>http://blog.udzura.jp/2010/02/11/some-tips-about-grub2/</link>
		<comments>http://blog.udzura.jp/2010/02/11/some-tips-about-grub2/#comments</comments>
		<pubDate>Thu, 11 Feb 2010 13:29:51 +0000</pubDate>
		<dc:creator>udzura</dc:creator>
				<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://blog.udzura.jp/?p=298</guid>
		<description><![CDATA[Ubuntu 9.10等，GRUB2を採用するディストリビューションも増えてきたこの頃，イカがお過ごしでしょうか。今日は，そんなGRUB2を色々いじっていて気づいたいくつかのtipsをメモしたりなんかします。
/boot/grub/menu.lst はもういない
GRUBのメニュー表示設定といえば /boot/grub/menu.lst という勢いで，インターネット上にもそのファイルの編集ノウハウがたくさん転がっていますが，GRUB2採用のUbuntu9.10にはそんなファイルありません。とっとと忘れましょう。

udzura@ubuntu-vaio:~$ ls /boot/grub/menu.lst
ls: /boot/grub/menu.lstにアクセスできません: No such file or directory
udzura@ubuntu-vaio:~$

デュアルブートしてるんだけど，起動画面で，正直Windowsが上にきてほしい
Windowsを入れているPCに，デュアルブート構成でUbuntuその他Linuxを入れると、「Ubuntu, Linux 2.6.31-19-generic」とかそういうエントリーが上のほうにきて，デフォルトでブート（ほっといてブート）するのもLinuxになります。 /boot/grub/menu.lst はもういないので，この順序を変えるにはどうすればいいの？？？
大丈夫！　実は簡単なんです。 /etc/grub.d と言うディレクトリをご覧ください

udzura@ubuntu-vaio:~$ ls /etc/grub.d
00_header        10_linux       30_os-prober  README
05_debian_theme  20_memtest86+  40_custom
udzura@ubuntu-vaio:~$

そう，実はこの順番でメニューが表示されるのです。10のlinux、20のmemtest、そしてやっと30のその他OS，と言う風に……
その他OSたるWindowsを上に持ち上げるには，30になってるos-proberを05より大きく10より小さい数に mv(1) すれば良いと思います。07とかいいんじゃないでしょうか。

1
sudo mv /etc/grub.d/30_os-prober /etc/grub.d/07_os-prober

そして、

1
sudo update-grub


udzura@ubuntu-vaio:~$ sudo update-grub
[sudo] password for udzura:
Generating grub.cfg ...
Found [...]]]></description>
			<content:encoded><![CDATA[<p>Ubuntu 9.10等，GRUB2を採用するディストリビューションも増えてきたこの頃，イカがお過ごしでしょうか。今日は，そんなGRUB2を色々いじっていて気づいたいくつかのtipsをメモしたりなんかします。</p>
<h3><code>/boot/grub/menu.lst</code> はもういない</h3>
<p>GRUBのメニュー表示設定といえば <code>/boot/grub/menu.lst</code> という勢いで，インターネット上にもそのファイルの編集ノウハウがたくさん転がっていますが，GRUB2採用のUbuntu9.10にはそんなファイルありません。とっとと忘れましょう。</p>
<pre>
udzura@ubuntu-vaio:~$ ls /boot/grub/menu.lst
ls: /boot/grub/menu.lstにアクセスできません: No such file or directory
udzura@ubuntu-vaio:~$
</pre>
<h3>デュアルブートしてるんだけど，起動画面で，正直Windowsが上にきてほしい</h3>
<p>Windowsを入れているPCに，デュアルブート構成でUbuntuその他Linuxを入れると、「Ubuntu, Linux 2.6.31-19-generic」とかそういうエントリーが上のほうにきて，デフォルトでブート（ほっといてブート）するのもLinuxになります。 <code>/boot/grub/menu.lst</code> はもういないので，この順序を変えるにはどうすればいいの？？？</p>
<p>大丈夫！　実は簡単なんです。 <code>/etc/grub.d</code> と言うディレクトリをご覧ください</p>
<pre>
udzura@ubuntu-vaio:~$ ls /etc/grub.d
00_header        10_linux       30_os-prober  README
05_debian_theme  20_memtest86+  40_custom
udzura@ubuntu-vaio:~$
</pre>
<p>そう，実はこの順番でメニューが表示されるのです。10のlinux、20のmemtest、そしてやっと30のその他OS，と言う風に……</p>
<p>その他OSたるWindowsを上に持ち上げるには，30になってるos-proberを05より大きく10より小さい数に <code>mv(1)</code> すれば良いと思います。07とかいいんじゃないでしょうか。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">mv</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>grub.d<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">30</span>_os-prober <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>grub.d<span style="color: #000000; font-weight: bold;">/</span>07_os-prober</pre></td></tr></table></div>

<p>そして、</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> update-grub</pre></td></tr></table></div>

<pre>
udzura@ubuntu-vaio:~$ sudo update-grub
[sudo] password for udzura:
Generating grub.cfg ...
Found Windows Vista (loader) on /dev/sda1
Found Windows Vista (loader) on /dev/sda2
Found linux image: /boot/vmlinuz-2.6.31-19-generic
Found initrd image: /boot/initrd.img-2.6.31-19-generic
Found linux image: /boot/vmlinuz-2.6.31-17-generic
Found initrd image: /boot/initrd.img-2.6.31-17-generic
Found linux image: /boot/vmlinuz-2.6.31-16-generic
Found initrd image: /boot/initrd.img-2.6.31-16-generic
Found linux image: /boot/vmlinuz-2.6.31-15-generic
Found initrd image: /boot/initrd.img-2.6.31-15-generic
Found linux image: /boot/vmlinuz-2.6.31-14-generic
Found initrd image: /boot/initrd.img-2.6.31-14-generic
Found memtest86+ image: /boot/memtest86+.bin
done
udzura@ubuntu-vaio:~$
</pre>
<p>Windowsが先に認識されたのが分かります。</p>
<h3>Linuxのカーネルのバージョンがたくさん出てきてうっとうしい</h3>
<p>上記のとおり，<code>update-grub(8)</code>はインストールされているLinuxのカーネルをすべて引っ張り出してしまいます。なので、アップデートが為されるたびにエントリーが追加されていきます。</p>
<p>でも、正直行って最新のカーネルしか使いませんよね。。</p>
<p>実は<code>update-grub(8)</code>は、 <code>/boot/grub/grub.cfg</code> を生成しているだけに過ぎないのです。なので、最悪、直接いじってしまえばいいのです。</p>
<p>ここで要注意なのは，<code>/boot/grub/grub.cfg</code> はrootを含む全ユーザに対してしてリードオンリー属性なので，いったん<code> chmod(1)</code> してあげないと，編集しても保存できなかったりとか何かと都合が悪いという事です。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">chmod</span> u+<span style="color: #c20cb9; font-weight: bold;">w</span> <span style="color: #000000; font-weight: bold;">/</span>boot<span style="color: #000000; font-weight: bold;">/</span>grub<span style="color: #000000; font-weight: bold;">/</span>grub.cfg
<span style="color: #c20cb9; font-weight: bold;">sudo</span> gedit <span style="color: #000000; font-weight: bold;">/</span>boot<span style="color: #000000; font-weight: bold;">/</span>grub<span style="color: #000000; font-weight: bold;">/</span>grub.cfg
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">chmod</span> u-w <span style="color: #000000; font-weight: bold;">/</span>boot<span style="color: #000000; font-weight: bold;">/</span>grub<span style="color: #000000; font-weight: bold;">/</span>grub.cfg</pre></td></tr></table></div>

<pre>
menuentry "Ubuntu, Linux 2.6.31-14-generic" {
        ......
}
</pre>
<p>↑のカギカッコの塊を好きなだけ消しちゃいましょう（※ 消しすぎると起動できなくなります！）。</p>
<h3>＊　＊　＊</h3>
<p>以上，実施した結果は無保証ですが。。お役に立てば何より。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.udzura.jp/2010/02/11/some-tips-about-grub2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://blog.udzura.jp/2010/02/11/some-tips-about-grub2/" />
	</item>
		<item>
		<title>またRails: たくさんincludeしてるときの検索</title>
		<link>http://blog.udzura.jp/2010/02/06/conditions-param-style-with-nested-include/</link>
		<comments>http://blog.udzura.jp/2010/02/06/conditions-param-style-with-nested-include/#comments</comments>
		<pubDate>Sat, 06 Feb 2010 04:44:07 +0000</pubDate>
		<dc:creator>udzura</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://blog.udzura.jp/?p=276</guid>
		<description><![CDATA[以下のようなモデルがあります（適当に考えたんですが、SNS的な何かを想定してみましょう）。

1
2
3
4
5
6
7
8
9
10
11
12
class Post &#60; ActiveRecord::Base
  belongs_to :member
end
&#160;
class Member &#60; ActiveRecord::Base
  belongs_to :job
  has_many :posts
end
&#160;
class Job &#60; ActiveRecord::Base
  has_many :members
end

「日付が2010年2月6日で、かつ書いた人の職業名が&#8221;Engineer”の人の日記一覧を抽出したい」んですけど、どうしましょう。なお、date属性はPostクラスに、job_name属性はJobクラスにくっついています。
なので、以下では全然ダメです。

1
2
3
4
5
6
Post.find&#40;:all,
  :conditions =&#62; &#123;
    :date =&#62; Date.new&#40;2010, 2, 6&#41;,
    :job_name =&#62; &#34;Engineer&#34;
  &#125;,
  :include =&#62; &#123;:member =&#62; :job&#125;&#41;

  SQL (0.2ms)   SET NAMES [...]]]></description>
			<content:encoded><![CDATA[<p>以下のようなモデルがあります（適当に考えたんですが、SNS的な何かを想定してみましょう）。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Post <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  belongs_to <span style="color:#ff3333; font-weight:bold;">:member</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> Member <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  belongs_to <span style="color:#ff3333; font-weight:bold;">:job</span>
  has_many <span style="color:#ff3333; font-weight:bold;">:posts</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> Job <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  has_many <span style="color:#ff3333; font-weight:bold;">:members</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>「日付が2010年2月6日で、かつ書いた人の職業名が&#8221;Engineer”の人の日記一覧を抽出したい」んですけど、どうしましょう。なお、date属性はPostクラスに、job_name属性はJobクラスにくっついています。</p>
<p>なので、以下では全然ダメです。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">Post.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:all</span>,
  <span style="color:#ff3333; font-weight:bold;">:conditions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span>
    <span style="color:#ff3333; font-weight:bold;">:date</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#CC00FF; font-weight:bold;">Date</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">2010</span>, <span style="color:#006666;">2</span>, <span style="color:#006666;">6</span><span style="color:#006600; font-weight:bold;">&#41;</span>,
    <span style="color:#ff3333; font-weight:bold;">:job_name</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Engineer&quot;</span>
  <span style="color:#006600; font-weight:bold;">&#125;</span>,
  <span style="color:#ff3333; font-weight:bold;">:include</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span>:member <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:job</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></td></tr></table></div>

<blockquote><p><code>  <strong style="text-decoration:underline;">SQL (0.2ms)</strong>   SET NAMES 'utf8'<br />
  <strong style="text-decoration:underline;">SQL (0.6ms)</strong>   SET SQL_AUTO_IS_NULL=0<br />
  <strong style="text-decoration:underline;">Post Load (0.0ms)</strong>   Mysql::Error: Unknown column 'posts.job_name' in 'where clause': SELECT * FROM `posts` WHERE (`posts`.`date` = '2010-01-31' AND `posts`.`job_name` = 'Engineer')<br />
</code></p></blockquote>
<p><span id="more-276"></span></p>
<p>正解は、</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">Post.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:all</span>,
  <span style="color:#ff3333; font-weight:bold;">:conditions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span>
    <span style="color:#996600;">&quot;posts.date&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#CC00FF; font-weight:bold;">Date</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">2010</span>, <span style="color:#006666;">2</span>, <span style="color:#006666;">6</span><span style="color:#006600; font-weight:bold;">&#41;</span>,
    <span style="color:#996600;">&quot;jobs.job_name&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Engineer&quot;</span>
  <span style="color:#006600; font-weight:bold;">&#125;</span>,
  <span style="color:#ff3333; font-weight:bold;">:include</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span>:member <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:job</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></td></tr></table></div>

<p>これで適切なフィールドを条件としたSQLが生成され、正しく抽出できます。</p>
<blockquote><p><code>  <strong style="text-decoration:underline;">Post Load Including Associations (0.4ms)</strong>   SELECT `posts`.`id` AS t0_r0, `posts`.`date` AS t0_r1, `posts`.`content` AS t0_r2, `posts`.`member_id` AS t0_r3, `posts`.`created_at` AS t0_r4, `posts`.`updated_at` AS t0_r5, `members`.`id` AS t1_r0, `members`.`name` AS t1_r1, `members`.`job_id` AS t1_r2, `members`.`created_at` AS t1_r3, `members`.`updated_at` AS t1_r4, `jobs`.`id` AS t2_r0, `jobs`.`job_name` AS t2_r1, `jobs`.`created_at` AS t2_r2, `jobs`.`updated_at` AS t2_r3 FROM `posts` LEFT OUTER JOIN `members` ON `members`.id = `posts`.member_id LEFT OUTER JOIN `jobs` ON `jobs`.id = `members`.job_id WHERE (`posts`.`date` = '2010-01-31' AND `jobs`.`job_name` = 'Engineer') </code></p></blockquote>
<p>「以下の書き方でダメなの？」　って最初思ったんですが、結論を言うとRails 2.3.5ではOKです。でも、Rails（というかactiverecord）2.2.2ではダメだった。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># RAILS_VERSION == 2.3.5</span>
Post.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:all</span>,
  <span style="color:#ff3333; font-weight:bold;">:conditions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span>
    <span style="color:#ff3333; font-weight:bold;">:date</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#CC00FF; font-weight:bold;">Date</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">2010</span>, <span style="color:#006666;">2</span>, <span style="color:#006666;">6</span><span style="color:#006600; font-weight:bold;">&#41;</span>,
    <span style="color:#996600;">&quot;jobs.job_name&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Engineer&quot;</span>
  <span style="color:#006600; font-weight:bold;">&#125;</span>,
  <span style="color:#ff3333; font-weight:bold;">:include</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span>:member <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:job</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></td></tr></table></div>

<p>バージョンはなるべく上げたいものです。</p>
<h3>おまけ: script/consoleで、ActiveRecordが生成するSQLを見たい</h3>
<p>script/consoleに入って、<a href="http://www.neeraj.name/blog/articles/738-show_sql-method-to-see-sql-statements-in-script-console">show_sql method to see sql statements in script/console</a> と言うページで紹介されているコードを適切にぶち込めば見られます。下のやつ。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.udzura.jp/2010/02/06/conditions-param-style-with-nested-include/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://blog.udzura.jp/2010/02/06/conditions-param-style-with-nested-include/" />
	</item>
		<item>
		<title>今日のワンライナー: 例えばログ検索で</title>
		<link>http://blog.udzura.jp/2010/02/04/todays-one-loner-for-log-grep/</link>
		<comments>http://blog.udzura.jp/2010/02/04/todays-one-loner-for-log-grep/#comments</comments>
		<pubDate>Thu, 04 Feb 2010 09:31:12 +0000</pubDate>
		<dc:creator>udzura</dc:creator>
				<category><![CDATA[Git]]></category>
		<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://blog.udzura.jp/?p=288</guid>
		<description><![CDATA[最近もっぱらGitを使用していますが、軽くて高機能ですし便利だと思います。でも今日はGitの話しません。
そんなGitのlogを見る際に、色々いじって手ワンライナーに便利そうなコマンドオプションを幾つか発見したので、鬼の首を取ったようにメモしときますね。
言っておきますが分かってる人には当然の内容ですよ。。

想定しているOSはLinux、GNU coreutilsとGNU grepです。他のOSに載っているやつで動くかは不明。
で。まずは単品の、

1
git log

これだとlessとかで見る事になります。それはそれでいいんですけど、

commit ac048f833ba3743a8d5ad313985f49571436ca57
Author: udzura &#60;udzura@hoge.udzura.jp&#62;
Date:   Thu Feb 4 15:05:13 2010 +0900

    バッチのtypo修正

commit ce9e4356c945f892b606b206534145845a9d6ce1
Author: udzura &#60;udzura@hoge.udzura.jp&#62;
Date:   Thu Feb 4 14:00:01 2010 +0900

    バッチ直した

commit 5197339ddbd701b1902c7d9b7ae9732c8b627750
Author: deadwinter &#60;deadwinter@hoge.udzura.jp&#62;
Date:   Tue Feb 2 13:37:56 2010 +0900

    app/models/hoge.rb typo直した

commit c10da34d0394415065efe4ee4d1e80eb3ae5bae7
Author: udzura &#60;udzura@hoge.udzura.jp&#62;
Date:   [...]]]></description>
			<content:encoded><![CDATA[<p>最近もっぱら<a href="http://git-scm.com/">Git</a>を使用していますが、軽くて高機能ですし便利だと思います。でも今日はGitの話しません。</p>
<p>そんなGitのlogを見る際に、色々いじって手ワンライナーに便利そうなコマンドオプションを幾つか発見したので、鬼の首を取ったようにメモしときますね。</p>
<p>言っておきますが分かってる人には当然の内容ですよ。。</p>
<p><span id="more-288"></span><br />
想定しているOSはLinux、GNU coreutilsとGNU grepです。他のOSに載っているやつで動くかは不明。</p>
<p>で。まずは単品の、</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">git log</pre></td></tr></table></div>

<p>これだとlessとかで見る事になります。それはそれでいいんですけど、</p>
<pre>
commit ac048f833ba3743a8d5ad313985f49571436ca57
Author: udzura &lt;udzura@hoge.udzura.jp&gt;
Date:   Thu Feb 4 15:05:13 2010 +0900

    バッチのtypo修正

commit ce9e4356c945f892b606b206534145845a9d6ce1
Author: udzura &lt;udzura@hoge.udzura.jp&gt;
Date:   Thu Feb 4 14:00:01 2010 +0900

    バッチ直した

commit 5197339ddbd701b1902c7d9b7ae9732c8b627750
Author: deadwinter &lt;deadwinter@hoge.udzura.jp&gt;
Date:   Tue Feb 2 13:37:56 2010 +0900

    app/models/hoge.rb typo直した

commit c10da34d0394415065efe4ee4d1e80eb3ae5bae7
Author: udzura &lt;udzura@hoge.udzura.jp&gt;
Date:   Mon Feb 1 12:28:56 2010 +0900
......
</pre>
<p>「ワンライナーで必要な情報だけシュバッ！　と抜き出したい」、UN*Xユーザならみんなそう思うかと考えられますので。</p>
<h3>行番号を付けたい</h3>
<p>「シュバッ」と抜き出す前段階として、表示に行番号を付けましょう。 <code style="text-decoration:underline;">cat -n</code> です。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">git log <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #660033;">-n</span></pre></td></tr></table></div>

<pre>
     1  commit ac048f833ba3743a8d5ad313985f49571436ca57
     2  Author: udzura &lt;udzura@hoge.udzura.jp&gt;
     3  Date:   Thu Feb 4 15:05:13 2010 +0900
     4
     5      バッチのtypo修正
     6
     7  commit ce9e4356c945f892b606b206534145845a9d6ce1
     8  Author: udzura &lt;udzura@hoge.udzura.jp&gt;
     9  Date:   Thu Feb 4 14:00:01 2010 +0900
    10
    11      バッチ直した
    12
    13  commit 5197339ddbd701b1902c7d9b7ae9732c8b627750
    14  Author: deadwinter &lt;deadwinter@hoge.udzura.jp&gt;
    15  Date:   Tue Feb 2 13:37:56 2010 +0900
    16
    17      app/models/hoge.rb typo直した
    18
    19  commit c10da34d0394415065efe4ee4d1e80eb3ae5bae7
    20  Author: udzura &lt;udzura@hoge.udzura.jp&gt;
    21  Date:   Mon Feb 1 12:28:56 2010 +0900
......
</pre>
<p>でもコレで</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">git log <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #660033;">-n</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> udzura<span style="color: #000000; font-weight: bold;">@</span>hoge.udzura.jp</pre></td></tr></table></div>

<pre>
     2  Author: udzura &lt;udzura@hoge.udzura.jp&gt;
     8  Author: udzura &lt;udzura@hoge.udzura.jp&gt;
    20  Author: udzura &lt;udzura@hoge.udzura.jp&gt;
......
</pre>
<p>とか抜き出しても、日付すら分かりませんね。。</p>
<h3>grepした行の回りも見たい</h3>
<p>grepには <code style="text-decoration:underline;">-C</code> オプションがあります。マッチした行の前後N行も一緒に表示してくれます。</p>
<p>git logとかはちょうど、ユーザ名の前後一行に必要な情報が入ってます。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">git log <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #660033;">-n</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-C1</span> udzura<span style="color: #000000; font-weight: bold;">@</span>hoge.udzura.jp</pre></td></tr></table></div>

<pre>
     1  commit ac048f833ba3743a8d5ad313985f49571436ca57
     2  Author: udzura &lt;udzura@hoge.udzura.jp&gt;
     3  Date:   Thu Feb 4 15:05:13 2010 +0900
--
     7  commit ce9e4356c945f892b606b206534145845a9d6ce1
     8  Author: udzura &lt;udzura@hoge.udzura.jp&gt;
     9  Date:   Thu Feb 4 14:00:01 2010 +0900
--
    19  commit c10da34d0394415065efe4ee4d1e80eb3ae5bae7
    20  Author: udzura &lt;udzura@hoge.udzura.jp&gt;
    21  Date:   Mon Feb 1 12:28:56 2010 +0900
......
</pre>
<p>ここからさらに、「2010年2月1日」のコミットを探したい時は、月日と年度が分かれてるせいで以下のようにしますね。grepコマンドでAND検索をする時は<strong style="text-decoration:underline;">ひたすらgrepをパイプで重ねる</strong>のが私のジャスティスです（OR検索したいアナタには、 <code style="text-decoration:underline;">-e</code> オプションというものがあります）。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">git log <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #660033;">-n</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-C1</span> udzura<span style="color: #000000; font-weight: bold;">@</span>hoge.udzura.jp <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #ff0000;">&quot;Thu Feb 4&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #000000;">2010</span></pre></td></tr></table></div>

<pre>
    21  Date:   Mon Feb 1 12:28:56 2010 +0900
</pre>
<p>今思ったんだけど曜日が入ってるんだから年度要らないかも。まあサンプルということで。</p>
<p>ともかく、見事、欲しい情報が「21行目あたりに入っているんだな」と判明しました。git logの仕様的には、きっと(21 &#8211; 2 = 19)～24行目の間に入っているのでしょう。</p>
<h3>N行目からM行を見たい</h3>
<p>こういう 21 &#8211; 2 行目から6行分を見たい、という際は、 <code style="text-decoration:underline;">head</code> と <code style="text-decoration:underline;">tail</code> を組み合わせるのが便利です。便利なんですよ。<strong style="text-decoration:underline;">ハイフン＋数字で「頭から|ケツから N 行を表示」</strong>という便利オプションを使いましょう。また、算数が面倒くさい向きには、 <code style="text-decoration:underline;">$((計算式))</code> という記法があって、これはこの中身の計算を展開してくれます。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">git log <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #660033;">-n</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">head</span> -$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">21</span> - <span style="color: #000000;">2</span> + <span style="color: #000000;">5</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">tail</span> <span style="color: #660033;">-6</span></pre></td></tr></table></div>

<pre>
    19  commit c10da34d0394415065efe4ee4d1e80eb3ae5bae7
    20  Author: udzura &lt;udzura@hoge.udzura.jp&gt;
    21  Date:   Mon Feb 1 12:28:56 2010 +0900
    22
    23      まあ適当に直した
    24
</pre>
<p>出来ましたね！</p>
<h3>まとめ</h3>
<p>以上をまとめ、バックスラッシュという便利記法を使用すると、以下のワンライナーになりますね。バックスラッシュ <code style="text-decoration:underline;">`コマンド`</code> は中身のコマンドを先に実行し、結果の出力を文字にして、その外側のコマンドを実行します。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">git log <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #660033;">-n</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">head</span> -$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">`</span>git log <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #660033;">-n</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-C1</span> udzura<span style="color: #000000; font-weight: bold;">@</span>hoge.udzura.jp <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #ff0000;">&quot;Mon Feb 1&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #000000;">2010</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cut</span> -f1<span style="color: #000000; font-weight: bold;">`</span> - <span style="color: #000000;">2</span> + <span style="color: #000000;">5</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">tail</span> <span style="color: #660033;">-6</span></pre></td></tr></table></div>

<p>すいません、まとめといいながらさらにどさくさにまぎれて <code style="text-decoration:underline;">cut -f</code> なるコマンドを混入させています。これは、空白区切り（デフォルト；区切りは <code style="text-decoration:underline;">-d</code> で指定）のテキストの、任意の列を切り出してくれます。</p>
<div class="tmkm-amazon-view">
	<p><a href="http://www.amazon.co.jp/%E3%80%90%E6%94%B9%E8%A8%82%E6%96%B0%E7%89%88%E3%80%91-Linux%E3%82%B3%E3%83%9E%E3%83%B3%E3%83%89-%E3%83%9D%E3%82%B1%E3%83%83%E3%83%88%E3%83%AA%E3%83%95%E3%82%A1%E3%83%AC%E3%83%B3%E3%82%B9-Pocket-Reference/dp/4774138169%3FSubscriptionId%3DAKIAI2SE5CBGHF3E5T4Q%26tag%3Dudzura1984-22%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D4774138169"><img src="http://ecx.images-amazon.com/images/I/51kWaJRnGVL._SL160_.jpg" border="0" alt="" /></a></p>
	<p><a href="http://www.amazon.co.jp/%E3%80%90%E6%94%B9%E8%A8%82%E6%96%B0%E7%89%88%E3%80%91-Linux%E3%82%B3%E3%83%9E%E3%83%B3%E3%83%89-%E3%83%9D%E3%82%B1%E3%83%83%E3%83%88%E3%83%AA%E3%83%95%E3%82%A1%E3%83%AC%E3%83%B3%E3%82%B9-Pocket-Reference/dp/4774138169%3FSubscriptionId%3DAKIAI2SE5CBGHF3E5T4Q%26tag%3Dudzura1984-22%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D4774138169">【改訂新版】 Linuxコマンド ポケットリファレンス (Pocket Reference)</a></p>
	<p><em>著者／訳者：</em>沓名 亮典 平山 智恵 </p>
	<p><em>出版社：</em>技術評論社( 2009-04-08 )</p>
	<p>単行本（ソフトカバー） ( 576 ページ )</p>
<hr class="tmkm-amazon-clear" /></div>
<h3>総括</h3>
<p><a href="http://www.moongift.jp/2008/12/tortoisegit/">TortoiseGit</a>って便利なんですね。</p>
<h3>総括２</h3>
<p>ワンライナーのコツは：</p>
<ul>
<li>コマンド一回一回ごとの「標準出力」の状態を把握する</li>
<li>バックスラッシュ記法を活用</li>
<li>その他、「<code>[条件] &#038;&#038; ～</code>」「<code>&#038;&#038;</code>」「<code>;</code>」や各種制御構造をフル活用</li>
</ul>
<p>とかな気がする。でも多分、シェルスクリプト書いたほうがいいよ。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.udzura.jp/2010/02/04/todays-one-loner-for-log-grep/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://blog.udzura.jp/2010/02/04/todays-one-loner-for-log-grep/" />
	</item>
		<item>
		<title>method_missing内でsuperすると</title>
		<link>http://blog.udzura.jp/2010/02/01/execute-super-in-method_missing/</link>
		<comments>http://blog.udzura.jp/2010/02/01/execute-super-in-method_missing/#comments</comments>
		<pubDate>Mon, 01 Feb 2010 07:29:59 +0000</pubDate>
		<dc:creator>udzura</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://blog.udzura.jp/?p=284</guid>
		<description><![CDATA[前回（ActiveRecord::Base#find(_by_id)? のお話）の続きのような。
あと、ちゃんとソース読んでないのでなんですが、「find_by_XXX」系メソッドは内部でmethod_missingを呼んでるとかいう話なので、当然ただのfindよりオーバヘッドがかかります（特にRuby 1.9系）。なんでもfind_by_idに置き換えるべきではないでしょう。
って書いたんだけど、たまたま当該箇所のソースを張ってらっしゃる方を見かけた。
……method_missing自体のオーバヘッドに加えて、class_eval までしてるし……（ｗ　Railsの黒魔術の恐ろしさの一環を垣間見ました。
で、ところどころ、なんかraiseのように使われているsuperが気になった。method_missingの中で呼んでるのはどういうこと？
以下、適当に試す。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
def method_missing&#40;meth_id, *args&#41;
  if match = /^power&#40;\d+&#41;$/.match&#40;meth_id.to_s&#41;
    $1.to_i ** 2
  else
    super
  end
end
#=&#62; nil
&#160;
power2
#=&#62; 4
&#160;
power10
#=&#62; 100
&#160;
hoge
#=&#62; NameError: undefined local variable or method `hoge' for main:Object
#        from (irb):12:in `method_missing'
#        from [...]]]></description>
			<content:encoded><![CDATA[<p>前回（<a href="http://blog.udzura.jp/2010/01/29/rails-diff-between-find-and-find-by-id/">ActiveRecord::Base#find(_by_id)? のお話</a>）の続きのような。</p>
<blockquote><p>あと、ちゃんとソース読んでないのでなんですが、「find_by_XXX」系メソッドは内部で<code>method_missing</code>を呼んでるとかいう話なので、当然ただの<code>find</code>よりオーバヘッドがかかります（特にRuby 1.9系）。なんでも<code>find_by_id</code>に置き換えるべきではないでしょう。</p></blockquote>
<p>って書いたんだけど、たまたま<a href="http://blog.livedoor.jp/sasata299/archives/51382345.html">当該箇所のソースを張ってらっしゃる方</a>を見かけた。</p>
<p>……method_missing自体のオーバヘッドに加えて、<code>class_eval</code> までしてるし……（ｗ　Railsの黒魔術の恐ろしさの一環を垣間見ました。</p>
<p>で、ところどころ、なんか<code>raise</code>のように使われている<code>super</code>が気になった。method_missingの中で呼んでるのはどういうこと？</p>
<p>以下、適当に試す。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> method_missing<span style="color:#006600; font-weight:bold;">&#40;</span>meth_id, <span style="color:#006600; font-weight:bold;">*</span>args<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">if</span> match = <span style="color:#006600; font-weight:bold;">/</span>^power<span style="color:#006600; font-weight:bold;">&#40;</span>\d<span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#41;</span>$<span style="color:#006600; font-weight:bold;">/</span>.<span style="color:#9900CC;">match</span><span style="color:#006600; font-weight:bold;">&#40;</span>meth_id.<span style="color:#9900CC;">to_s</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    $1.<span style="color:#9900CC;">to_i</span> <span style="color:#006600; font-weight:bold;">**</span> <span style="color:#006666;">2</span>
  <span style="color:#9966CC; font-weight:bold;">else</span>
    <span style="color:#9966CC; font-weight:bold;">super</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#008000; font-style:italic;">#=&gt; nil</span>
&nbsp;
power2
<span style="color:#008000; font-style:italic;">#=&gt; 4</span>
&nbsp;
power10
<span style="color:#008000; font-style:italic;">#=&gt; 100</span>
&nbsp;
hoge
<span style="color:#008000; font-style:italic;">#=&gt; NameError: undefined local variable or method `hoge' for main:Object</span>
<span style="color:#008000; font-style:italic;">#        from (irb):12:in `method_missing'</span>
<span style="color:#008000; font-style:italic;">#        from (irb):20</span>
<span style="color:#008000; font-style:italic;">#        from :0</span></pre></td></tr></table></div>

<p>メソッドが無いときのデフォルトの挙動、<code>NameError</code> が出る。</p>
<p>アレか。<code>method_missing</code>はもともと、<code>NameError</code>を<code>raise</code>するだけのメソッドとして定義されていて、それをオーバーライドしてるイメージ？　なの？</p>
<p><a href="http://www.ruby-lang.org/ja/man/html/Object.html#method_missing">そうでした。</a></p>
<p><a href="http://github.com/shyouhei/ruby/blob/trunk/vm_eval.c#L476">ソースでも、多分そんな感じ。<code>rb_eNameError</code>が確かに呼ばれてるみたい。</a></p>
<p>「マニュアルちゃんと読め。ソース読め。」という結論になりそうな。</p>
<div class="tmkm-amazon-view">
	<p><a href="http://www.amazon.co.jp/Ruby%E6%8A%80%E8%A1%93%E8%80%85%E8%AA%8D%E5%AE%9A%E8%A9%A6%E9%A8%93-%E5%85%AC%E5%BC%8F%E3%82%AC%E3%82%A4%E3%83%89-ITpro-BOOKs-%E4%BC%8A%E8%97%A4%E5%BF%A0%E3%83%86%E3%82%AF%E3%83%8E%E3%82%BD%E3%83%AA%E3%83%A5%E3%83%BC%E3%82%B7%E3%83%A7%E3%83%B3%E3%82%BA/dp/4822234304%3FSubscriptionId%3DAKIAI2SE5CBGHF3E5T4Q%26tag%3Dudzura1984-22%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D4822234304"><img src="http://ecx.images-amazon.com/images/I/41D9PRkspEL._SL160_.jpg" border="0" alt="" /></a></p>
	<p><a href="http://www.amazon.co.jp/Ruby%E6%8A%80%E8%A1%93%E8%80%85%E8%AA%8D%E5%AE%9A%E8%A9%A6%E9%A8%93-%E5%85%AC%E5%BC%8F%E3%82%AC%E3%82%A4%E3%83%89-ITpro-BOOKs-%E4%BC%8A%E8%97%A4%E5%BF%A0%E3%83%86%E3%82%AF%E3%83%8E%E3%82%BD%E3%83%AA%E3%83%A5%E3%83%BC%E3%82%B7%E3%83%A7%E3%83%B3%E3%82%BA/dp/4822234304%3FSubscriptionId%3DAKIAI2SE5CBGHF3E5T4Q%26tag%3Dudzura1984-22%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D4822234304">Ruby技術者認定試験 公式ガイド (ITpro BOOKs)</a></p>
	<p><em>著者／訳者：</em>伊藤忠テクノソリューションズ</p>
	<p><em>出版社：</em>日経BP社( 2009-03-25 )</p>
	<p>単行本（ソフトカバー） ( 256 ページ )</p>
<hr class="tmkm-amazon-clear" /></div>
<p>↑、「いつか受ける」と思って早半年……。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.udzura.jp/2010/02/01/execute-super-in-method_missing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://blog.udzura.jp/2010/02/01/execute-super-in-method_missing/" />
	</item>
		<item>
		<title>ActiveRecord::Base#find(_by_id)? のお話</title>
		<link>http://blog.udzura.jp/2010/01/29/rails-diff-between-find-and-find-by-id/</link>
		<comments>http://blog.udzura.jp/2010/01/29/rails-diff-between-find-and-find-by-id/#comments</comments>
		<pubDate>Fri, 29 Jan 2010 06:38:52 +0000</pubDate>
		<dc:creator>udzura</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://blog.udzura.jp/?p=270</guid>
		<description><![CDATA[最近Ruby on Railsばかりなので、今回もRailsのお話。
何も考えずに特定のIDのレコードを見つける際は、「ActiveRecord::Base#find」という超基本メソッドを使うけれど、同時に、XXXというフィールドに対して「ActiveRecord::Base#find_by_XXX」なるメソッドも定義されているので、「ActiveRecord::Base#find_by_id」でも同じようにidに紐付けて見つけ出せる。
以上は当たり前の話だが、この同じような２つ、重要なところが違う。すなわち、そのIDに紐付くレコードが存在しない場合。
「ActiveRecord::Base#find」は例外が生じる。

1
2
3
4
5
6
7
Post.find&#40;1&#41;
#=&#62; ActiveRecord::RecordNotFound: Couldn't find Post with ID=1
#	from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:1586:in `find_one'
#	from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:1569:in `find_from_ids'
#	from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:616:in `find'
#	from (irb):6
#	from :0

「ActiveRecord::Base#find_by_id」は nil を返す。

1
2
Post.find_by_id&#40;1&#41;
#=&#62; nil

このお話はそれなりに有名なようですが、find_by_idを使えば、例えばこういうコードが書けて気分がいいですね。


1
&#40;m = ItemMaster.find_by_id&#40;params&#91;:id&#93;&#41;&#41; ? m.item_name : params&#91;:id&#93;

このワンライナーが多発するぐらいなら、item_master.rb にクラスメソッドを定義するべきだとは思うけど。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class PostController &#60; ApplicationController
  #例えばブログで
  def show
    @post = Post.find_by_id&#40;params&#91;:id&#93;&#41;
    if !@post
      flash&#91;:notice&#93; = &#34;ブログ記事が存在しません: #{params[:id]}&#34;
 [...]]]></description>
			<content:encoded><![CDATA[<p>最近Ruby on Railsばかりなので、今回もRailsのお話。</p>
<p>何も考えずに特定のIDのレコードを見つける際は、「<code>ActiveRecord::Base#find</code>」という超基本メソッドを使うけれど、同時に、XXXというフィールドに対して「<code>ActiveRecord::Base#find_by_XXX</code>」なるメソッドも定義されているので、「<code>ActiveRecord::Base#find_by_id</code>」でも同じようにidに紐付けて見つけ出せる。</p>
<p>以上は当たり前の話だが、この同じような２つ、重要なところが違う。すなわち、そのIDに紐付くレコードが<strong>存在しない</strong>場合。</p>
<p>「<code>ActiveRecord::Base#find</code>」は例外が生じる。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">Post.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#008000; font-style:italic;">#=&gt; ActiveRecord::RecordNotFound: Couldn't find Post with ID=1</span>
<span style="color:#008000; font-style:italic;">#	from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:1586:in `find_one'</span>
<span style="color:#008000; font-style:italic;">#	from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:1569:in `find_from_ids'</span>
<span style="color:#008000; font-style:italic;">#	from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:616:in `find'</span>
<span style="color:#008000; font-style:italic;">#	from (irb):6</span>
<span style="color:#008000; font-style:italic;">#	from :0</span></pre></td></tr></table></div>

<p>「<code>ActiveRecord::Base#find_by_id</code>」は <code>nil</code> を返す。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">Post.<span style="color:#9900CC;">find_by_id</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#008000; font-style:italic;">#=&gt; nil</span></pre></td></tr></table></div>

<p><a href="http://www.google.co.jp/search?q=rails+find+find_by_id">このお話はそれなりに有名なようです</a>が、find_by_idを使えば、例えばこういうコードが書けて気分がいいですね。</p>
<p><span id="more-270"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&#40;</span>m = ItemMaster.<span style="color:#9900CC;">find_by_id</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:id</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> ? m.<span style="color:#9900CC;">item_name</span> : params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:id</span><span style="color:#006600; font-weight:bold;">&#93;</span></pre></td></tr></table></div>

<p>このワンライナーが多発するぐらいなら、<code>item_master.rb</code> にクラスメソッドを定義するべきだとは思うけど。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> PostController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController
  <span style="color:#008000; font-style:italic;">#例えばブログで</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> show
    <span style="color:#0066ff; font-weight:bold;">@post</span> = Post.<span style="color:#9900CC;">find_by_id</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:id</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">if</span> !@post
      flash<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:notice</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">&quot;ブログ記事が存在しません: #{params[:id]}&quot;</span>
      redirect_to <span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:index</span>
      <span style="color:#0000FF; font-weight:bold;">return</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    respond_to <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>format<span style="color:#006600; font-weight:bold;">|</span>
      <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">html</span> 
      <span style="color:#008000; font-style:italic;">#...</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>こっちは幾分か実用的な気がする。IDが存在しないだけで <del>503</del> 500 になって精神衛生状態が悪くなるのを回避できます。むしろ404を返してもいい（参考: <a href="http://http://d.hatena.ne.jp/NeoCat/20080604/1212599193">http://d.hatena.ne.jp/NeoCat/20080604/1212599193</a>）。</p>
<p>この挙動は、少なくともRails 2.2.2とRails 2.3.5でこうなることを確認しています、よしなに。</p>
<p>あと、ちゃんとソース読んでないのでなんですが、「find_by_XXX」系メソッドは内部でmethod_missingを呼んでるとかいう話なので、当然ただのfindよりオーバヘッドがかかります（特にRuby 1.9系）。なんでもfind_by_idに置き換えるべきではないでしょう。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.udzura.jp/2010/01/29/rails-diff-between-find-and-find-by-id/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://blog.udzura.jp/2010/01/29/rails-diff-between-find-and-find-by-id/" />
	</item>
		<item>
		<title>Railsのdb:migrateは、最新バージョンより古くてもさかのぼる</title>
		<link>http://blog.udzura.jp/2010/01/22/rails-db-migration-tips/</link>
		<comments>http://blog.udzura.jp/2010/01/22/rails-db-migration-tips/#comments</comments>
		<pubDate>Fri, 22 Jan 2010 05:06:56 +0000</pubDate>
		<dc:creator>udzura</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://blog.udzura.jp/?p=265</guid>
		<description><![CDATA[どういうことかと言うと、
rails(1) でつくる。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
&#91;udzura@udzra.jp rails&#93;$ rails -v
Rails 2.3.5
&#91;udzura@udzra.jp rails&#93;$ rails -d mysql migtest
      create
      create  app/controllers
      create  app/helpers
      create  app/models
      create  app/views/layouts
      create [...]]]></description>
			<content:encoded><![CDATA[<p>どういうことかと言うと、</p>
<p><code>rails(1)</code> でつくる。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>udzura<span style="color: #000000; font-weight: bold;">@</span>udzra.jp rails<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ rails <span style="color: #660033;">-v</span>
Rails 2.3.5
<span style="color: #7a0874; font-weight: bold;">&#91;</span>udzura<span style="color: #000000; font-weight: bold;">@</span>udzra.jp rails<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ rails <span style="color: #660033;">-d</span> mysql migtest
      create
      create  app<span style="color: #000000; font-weight: bold;">/</span>controllers
      create  app<span style="color: #000000; font-weight: bold;">/</span>helpers
      create  app<span style="color: #000000; font-weight: bold;">/</span>models
      create  app<span style="color: #000000; font-weight: bold;">/</span>views<span style="color: #000000; font-weight: bold;">/</span>layouts
      create  config<span style="color: #000000; font-weight: bold;">/</span>environments
...
      create  log<span style="color: #000000; font-weight: bold;">/</span>server.log
      create  log<span style="color: #000000; font-weight: bold;">/</span>production.log
      create  log<span style="color: #000000; font-weight: bold;">/</span>development.log
      create  log<span style="color: #000000; font-weight: bold;">/</span>test.log</pre></td></tr></table></div>

<p>マイグレーションを作る。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>udzura<span style="color: #000000; font-weight: bold;">@</span>udzra.jp rails<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #7a0874; font-weight: bold;">cd</span> migtest<span style="color: #000000; font-weight: bold;">/</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>udzura<span style="color: #000000; font-weight: bold;">@</span>udzra.jp migtest<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ script<span style="color: #000000; font-weight: bold;">/</span>generate model User name:string
      exists  app<span style="color: #000000; font-weight: bold;">/</span>models<span style="color: #000000; font-weight: bold;">/</span>
      exists  test<span style="color: #000000; font-weight: bold;">/</span>unit<span style="color: #000000; font-weight: bold;">/</span>
      exists  test<span style="color: #000000; font-weight: bold;">/</span>fixtures<span style="color: #000000; font-weight: bold;">/</span>
      create  app<span style="color: #000000; font-weight: bold;">/</span>models<span style="color: #000000; font-weight: bold;">/</span>user.rb
      create  test<span style="color: #000000; font-weight: bold;">/</span>unit<span style="color: #000000; font-weight: bold;">/</span>user_test.rb
      create  test<span style="color: #000000; font-weight: bold;">/</span>fixtures<span style="color: #000000; font-weight: bold;">/</span>users.yml
      create  db<span style="color: #000000; font-weight: bold;">/</span>migrate
      create  db<span style="color: #000000; font-weight: bold;">/</span>migrate<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">20100122041512</span>_create_users.rb
<span style="color: #7a0874; font-weight: bold;">&#91;</span>udzura<span style="color: #000000; font-weight: bold;">@</span>udzra.jp migtest<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ script<span style="color: #000000; font-weight: bold;">/</span>generate model Blog user_id:integer entry:text
      exists  app<span style="color: #000000; font-weight: bold;">/</span>models<span style="color: #000000; font-weight: bold;">/</span>
      exists  test<span style="color: #000000; font-weight: bold;">/</span>unit<span style="color: #000000; font-weight: bold;">/</span>
      exists  test<span style="color: #000000; font-weight: bold;">/</span>fixtures<span style="color: #000000; font-weight: bold;">/</span>
      create  app<span style="color: #000000; font-weight: bold;">/</span>models<span style="color: #000000; font-weight: bold;">/</span>blog.rb
      create  test<span style="color: #000000; font-weight: bold;">/</span>unit<span style="color: #000000; font-weight: bold;">/</span>blog_test.rb
      create  test<span style="color: #000000; font-weight: bold;">/</span>fixtures<span style="color: #000000; font-weight: bold;">/</span>blogs.yml
      exists  db<span style="color: #000000; font-weight: bold;">/</span>migrate
      create  db<span style="color: #000000; font-weight: bold;">/</span>migrate<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">20100122041555</span>_create_blogs.rb</pre></td></tr></table></div>

<p>2099年とかすっごい未来にリネームする。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>udzura<span style="color: #000000; font-weight: bold;">@</span>udzra.jp migtest<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #660033;">-l</span> db<span style="color: #000000; font-weight: bold;">/</span>migrate<span style="color: #000000; font-weight: bold;">/</span>
合計 <span style="color: #000000;">8</span>
<span style="color: #660033;">-rw-rw-r--</span>  <span style="color: #000000;">1</span> udzura udzura <span style="color: #000000;">193</span>  <span style="color: #000000;">1</span>月 <span style="color: #000000;">22</span> <span style="color: #000000;">13</span>:<span style="color: #000000;">15</span> <span style="color: #000000;">20100122041512</span>_create_users.rb
<span style="color: #660033;">-rw-rw-r--</span>  <span style="color: #000000;">1</span> udzura udzura <span style="color: #000000;">217</span>  <span style="color: #000000;">1</span>月 <span style="color: #000000;">22</span> <span style="color: #000000;">13</span>:<span style="color: #000000;">15</span> <span style="color: #000000;">20100122041555</span>_create_blogs.rb
<span style="color: #7a0874; font-weight: bold;">&#91;</span>udzura<span style="color: #000000; font-weight: bold;">@</span>udzra.jp migtest<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #c20cb9; font-weight: bold;">mv</span> db<span style="color: #000000; font-weight: bold;">/</span>migrate<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">20100122041555</span>_create_blogs.rb \
  db<span style="color: #000000; font-weight: bold;">/</span>migrate<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">20990122041555</span>_create_blogs.rb</pre></td></tr></table></div>

<p>マイグレートする。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>udzura<span style="color: #000000; font-weight: bold;">@</span>udzra.jp migtest<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ rake db:create
<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>web-devel<span style="color: #000000; font-weight: bold;">/</span>rails<span style="color: #000000; font-weight: bold;">/</span>migtest<span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>udzura<span style="color: #000000; font-weight: bold;">@</span>udzra.jp migtest<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ rake db:migrate
<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>web-devel<span style="color: #000000; font-weight: bold;">/</span>rails<span style="color: #000000; font-weight: bold;">/</span>migtest<span style="color: #7a0874; font-weight: bold;">&#41;</span>
==  CreateUsers: migrating ====================================================
<span style="color: #660033;">--</span> create_table<span style="color: #7a0874; font-weight: bold;">&#40;</span>:<span style="color: #c20cb9; font-weight: bold;">users</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
   -<span style="color: #000000; font-weight: bold;">&gt;</span> 0.0038s
==  CreateUsers: migrated <span style="color: #7a0874; font-weight: bold;">&#40;</span>0.0041s<span style="color: #7a0874; font-weight: bold;">&#41;</span> ===========================================
&nbsp;
==  CreateBlogs: migrating ====================================================
<span style="color: #660033;">--</span> create_table<span style="color: #7a0874; font-weight: bold;">&#40;</span>:blogs<span style="color: #7a0874; font-weight: bold;">&#41;</span>
   -<span style="color: #000000; font-weight: bold;">&gt;</span> 0.0032s
==  CreateBlogs: migrated <span style="color: #7a0874; font-weight: bold;">&#40;</span>0.0035s<span style="color: #7a0874; font-weight: bold;">&#41;</span> ===========================================
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>udzura<span style="color: #000000; font-weight: bold;">@</span>udzra.jp migtest<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ mysql <span style="color: #660033;">-uroot</span> <span style="color: #660033;">-pxxxx</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;SHOW TABLES&quot;</span> migtest_development
+-------------------------------+
<span style="color: #000000; font-weight: bold;">|</span> Tables_in_migtest_development <span style="color: #000000; font-weight: bold;">|</span>
+-------------------------------+
<span style="color: #000000; font-weight: bold;">|</span> blogs                         <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">|</span> schema_migrations             <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">users</span>                         <span style="color: #000000; font-weight: bold;">|</span>
+-------------------------------+
<span style="color: #7a0874; font-weight: bold;">&#91;</span>udzura<span style="color: #000000; font-weight: bold;">@</span>udzra.jp migtest<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ rake db:version
<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>web-devel<span style="color: #000000; font-weight: bold;">/</span>rails<span style="color: #000000; font-weight: bold;">/</span>migtest<span style="color: #7a0874; font-weight: bold;">&#41;</span>
Current version: <span style="color: #000000;">20990122041555</span></pre></td></tr></table></div>

<p>マイグレーションを加える。当然、さっき作った二つのマイグレーションの間に入ります。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>udzura<span style="color: #000000; font-weight: bold;">@</span>udzra.jp migtest<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ script<span style="color: #000000; font-weight: bold;">/</span>generate model UserFriend user_id:integer friend_user_id:integer
      exists  app<span style="color: #000000; font-weight: bold;">/</span>models<span style="color: #000000; font-weight: bold;">/</span>
      exists  test<span style="color: #000000; font-weight: bold;">/</span>unit<span style="color: #000000; font-weight: bold;">/</span>
      exists  test<span style="color: #000000; font-weight: bold;">/</span>fixtures<span style="color: #000000; font-weight: bold;">/</span>
      create  app<span style="color: #000000; font-weight: bold;">/</span>models<span style="color: #000000; font-weight: bold;">/</span>user_friend.rb
      create  test<span style="color: #000000; font-weight: bold;">/</span>unit<span style="color: #000000; font-weight: bold;">/</span>user_friend_test.rb
      create  test<span style="color: #000000; font-weight: bold;">/</span>fixtures<span style="color: #000000; font-weight: bold;">/</span>user_friends.yml
      exists  db<span style="color: #000000; font-weight: bold;">/</span>migrate
      create  db<span style="color: #000000; font-weight: bold;">/</span>migrate<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">20100122041954</span>_create_user_friends.rb
<span style="color: #7a0874; font-weight: bold;">&#91;</span>udzura<span style="color: #000000; font-weight: bold;">@</span>udzra.jp migtest<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #660033;">-l</span> db<span style="color: #000000; font-weight: bold;">/</span>migrate<span style="color: #000000; font-weight: bold;">/</span>
合計 <span style="color: #000000;">12</span>
<span style="color: #660033;">-rw-rw-r--</span>  <span style="color: #000000;">1</span> udzura udzura <span style="color: #000000;">193</span>  <span style="color: #000000;">1</span>月 <span style="color: #000000;">22</span> <span style="color: #000000;">13</span>:<span style="color: #000000;">15</span> <span style="color: #000000;">20100122041512</span>_create_users.rb
<span style="color: #660033;">-rw-rw-r--</span>  <span style="color: #000000;">1</span> udzura udzura <span style="color: #000000;">249</span>  <span style="color: #000000;">1</span>月 <span style="color: #000000;">22</span> <span style="color: #000000;">13</span>:<span style="color: #000000;">19</span> <span style="color: #000000;">20100122041954</span>_create_user_friends.rb
<span style="color: #660033;">-rw-rw-r--</span>  <span style="color: #000000;">1</span> udzura udzura <span style="color: #000000;">217</span>  <span style="color: #000000;">1</span>月 <span style="color: #000000;">22</span> <span style="color: #000000;">13</span>:<span style="color: #000000;">15</span> <span style="color: #000000;">20990122041555</span>_create_blogs.rb</pre></td></tr></table></div>

<p>こういう場合でもさかのぼってマイグレーションを実行してくれます。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>udzura<span style="color: #000000; font-weight: bold;">@</span>udzra.jp migtest<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ rake db:migrate
<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>web-devel<span style="color: #000000; font-weight: bold;">/</span>rails<span style="color: #000000; font-weight: bold;">/</span>migtest<span style="color: #7a0874; font-weight: bold;">&#41;</span>
==  CreateUserFriends: migrating ==============================================
<span style="color: #660033;">--</span> create_table<span style="color: #7a0874; font-weight: bold;">&#40;</span>:user_friends<span style="color: #7a0874; font-weight: bold;">&#41;</span>
   -<span style="color: #000000; font-weight: bold;">&gt;</span> 0.0067s
==  CreateUserFriends: migrated <span style="color: #7a0874; font-weight: bold;">&#40;</span>0.0071s<span style="color: #7a0874; font-weight: bold;">&#41;</span> =====================================
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>udzura<span style="color: #000000; font-weight: bold;">@</span>udzra.jp migtest<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ mysql <span style="color: #660033;">-uroot</span> <span style="color: #660033;">-pxxxx</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;SHOW TABLES&quot;</span> migtest_development
+-------------------------------+
<span style="color: #000000; font-weight: bold;">|</span> Tables_in_migtest_development <span style="color: #000000; font-weight: bold;">|</span>
+-------------------------------+
<span style="color: #000000; font-weight: bold;">|</span> blogs                         <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">|</span> schema_migrations             <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">|</span> user_friends                  <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">users</span>                         <span style="color: #000000; font-weight: bold;">|</span>
+-------------------------------+</pre></td></tr></table></div>

<h3>何が嬉しいの？</h3>
<p>例えば、実表のマイグレーションを通常の作成時の日次で、ビュー表を作成するマイグレーションを「2099年」の日次で、という風にしておくと、「必ずすべての実表が作られてからビュー表が定義される」ことが保障され、なおかつ、ビュー表の定義を変更した際、作り直しが <code>rake(1)</code> で容易にできます。</p>
<p>ポイントは <code>rake db:migrate:down VERSION=2099XXXXXXXXXX</code> 。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>udzura<span style="color: #000000; font-weight: bold;">@</span>udzra.jp migtest<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ rake db:migrate:down <span style="color: #007800;">VERSION</span>=<span style="color: #000000;">20990122041555</span>
<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>web-devel<span style="color: #000000; font-weight: bold;">/</span>rails<span style="color: #000000; font-weight: bold;">/</span>migtest<span style="color: #7a0874; font-weight: bold;">&#41;</span>
==  CreateViewBlogs: reverting ====================================================
<span style="color: #660033;">--</span> drop_table<span style="color: #7a0874; font-weight: bold;">&#40;</span>:view_blogs<span style="color: #7a0874; font-weight: bold;">&#41;</span>
   -<span style="color: #000000; font-weight: bold;">&gt;</span> 0.0277s
==  CreateViewBlogs: reverted <span style="color: #7a0874; font-weight: bold;">&#40;</span>0.0280s<span style="color: #7a0874; font-weight: bold;">&#41;</span> ===========================================
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>udzura<span style="color: #000000; font-weight: bold;">@</span>udzra.jp migtest<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ rake db:migrate
<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>web-devel<span style="color: #000000; font-weight: bold;">/</span>rails<span style="color: #000000; font-weight: bold;">/</span>migtest<span style="color: #7a0874; font-weight: bold;">&#41;</span>
==  CreateViewBlogs: migrating ====================================================
<span style="color: #660033;">--</span> create_table<span style="color: #7a0874; font-weight: bold;">&#40;</span>:view_blogs<span style="color: #7a0874; font-weight: bold;">&#41;</span>
   -<span style="color: #000000; font-weight: bold;">&gt;</span> 0.0472s
==  CreateViewBlogs: migrated <span style="color: #7a0874; font-weight: bold;">&#40;</span>0.0475s<span style="color: #7a0874; font-weight: bold;">&#41;</span> ===========================================
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>udzura<span style="color: #000000; font-weight: bold;">@</span>udzra.jp migtest<span style="color: #7a0874; font-weight: bold;">&#93;</span>$</pre></td></tr></table></div>

<p>RailsでCreate Viewしたい際は、普通に <code>execute</code> しないといけません。なので、そもそも「RailsでView表使うとかあほか」って方もいると思う。引き出しの一つとして。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.udzura.jp/2010/01/22/rails-db-migration-tips/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://blog.udzura.jp/2010/01/22/rails-db-migration-tips/" />
	</item>
		<item>
		<title>開いたWindowの存在を検出</title>
		<link>http://blog.udzura.jp/2010/01/19/detect-if-the-window-exists/</link>
		<comments>http://blog.udzura.jp/2010/01/19/detect-if-the-window-exists/#comments</comments>
		<pubDate>Tue, 19 Jan 2010 11:20:04 +0000</pubDate>
		<dc:creator>udzura</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://blog.udzura.jp/?p=252</guid>
		<description><![CDATA[友達が困ってるので書いた。
ポップアップでウィンドウを開く時、二重でオープンしたり、開いてるウィンドウを間違えてリフレッシュしたりしないためにはどうするかということだと思う。
要するに普通にwindow.openで開けばいいのだが、このメソッドの返り値は開いた先のwindowなので、変数に閉じ込めればよい。そうすれば「closed」というプロパティで開いてるか閉じてるかが検出可能。

1
2
3
4
5
6
var myWin = window.open&#40;'http://blog.udzura.jp', 'mywindow1'&#41;;
alert&#40;myWin.closed&#41;;
//=&#62; false
//開いたウィンドウを閉じれば
alert&#40;myWin.closed&#41;;
//=&#62; true

実際はグローバル変数とか微妙なので、せめてクロージャを使うとスマートだと思う。
やっつけなりに実装してみたソースは以下の如し。サンプルサイトもあるよ。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function setOpener&#40;&#41;&#123;
  var myWin = null;
  var openOneAndOnlyWindow = function&#40;e&#41;&#123;
    if&#40;e&#41; e.preventDefault&#40;&#41;;
    if&#40;!myWin &#124;&#124; myWin.closed&#41;&#123;
      alert&#40;&#34;window does not exist!&#34;&#41;;
      myWin = window.open&#40;'http://blog.udzura.jp', 'mywindow1'&#41;;
    &#125; else &#123;
   [...]]]></description>
			<content:encoded><![CDATA[<p>友達が困ってるので書いた。</p>
<p>ポップアップでウィンドウを開く時、二重でオープンしたり、開いてるウィンドウを間違えてリフレッシュしたりしないためにはどうするかということだと思う。</p>
<p>要するに普通にwindow.openで開けばいいのだが、このメソッドの返り値は開いた先のwindowなので、変数に閉じ込めればよい。そうすれば「closed」というプロパティで開いてるか閉じてるかが検出可能。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> myWin <span style="color: #339933;">=</span> window.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'http://blog.udzura.jp'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'mywindow1'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>myWin.<span style="color: #660066;">closed</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">//=&gt; false</span>
<span style="color: #006600; font-style: italic;">//開いたウィンドウを閉じれば</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>myWin.<span style="color: #660066;">closed</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">//=&gt; true</span></pre></td></tr></table></div>

<p>実際はグローバル変数とか微妙なので、せめてクロージャを使うとスマートだと思う。</p>
<p>やっつけなりに実装してみたソースは以下の如し。<a href="http://udzura.jp/test_site/test.html">サンプルサイト</a>もあるよ。</p>
<p><span id="more-252"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> setOpener<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> myWin <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> openOneAndOnlyWindow <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> e.<span style="color: #660066;">preventDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>myWin <span style="color: #339933;">||</span> myWin.<span style="color: #660066;">closed</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;window does not exist!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      myWin <span style="color: #339933;">=</span> window.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'http://blog.udzura.jp'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'mywindow1'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;window exists...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">return</span> openOneAndOnlyWindow<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
window.<span style="color: #000066;">onload</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;opener_link&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">onclick</span> <span style="color: #339933;">=</span> setOpener<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;script</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;detect_window.js&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h1<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;#&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;opener_link&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Open Just One Window<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/h1<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>ポップアップウィンドウが開いてる/閉じてる状態でリンクをクリックしたりして遊んでください。なお、ファイヤーフォックス（>=3.5.7）とインタネットエクスプローラ（>=7）でしか確認してません。</p>
<p>さらに言うと、僕は普段ろくにJavaScript書いてなかったりする（<a href="http://jp.rubyist.net/magazine/?0014-RubyOnRails">RJS便利だよRJS</a>派）。。</p>
<h3>参考サイト</h3>
<ul>
<li><a href="http://www.tohoho-web.com/js/window.htm#open">http://www.tohoho-web.com/js/window.htm#open</a></li>
<li><a href="http://f32.aaa.livedoor.jp/~azusa/?t=js&#038;p=window_openclose#a_closed">http://f32.aaa.livedoor.jp/~azusa/?t=js&#038;p=window_openclose#a_closed</a></li>
<li><a href="http://0xcc.net/blog/archives/000040.html">http://0xcc.net/blog/archives/000040.html （クロージャのソース参考にした）</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.udzura.jp/2010/01/19/detect-if-the-window-exists/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://blog.udzura.jp/2010/01/19/detect-if-the-window-exists/" />
	</item>
		<item>
		<title>僕の考えたカリー化</title>
		<link>http://blog.udzura.jp/2010/01/14/my-own-thought-about-currying/</link>
		<comments>http://blog.udzura.jp/2010/01/14/my-own-thought-about-currying/#comments</comments>
		<pubDate>Thu, 14 Jan 2010 01:21:07 +0000</pubDate>
		<dc:creator>udzura</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[勉強記録]]></category>
		<category><![CDATA[＜技術一般＞]]></category>

		<guid isPermaLink="false">http://blog.udzura.jp/?p=243</guid>
		<description><![CDATA[カリー化とは
複数の引数をとる関数を、引数が「もとの関数の最初の引数」で戻り値が「もとの関数の残りの引数を取り結果を返す関数」であるような関数にすること。

http://wiki.onakasuita.org/pukiwiki/?%E3%82%AB%E3%83%AA%E3%83%BC%E5%8C%96
http://ja.wikipedia.org/wiki/%E3%82%AB%E3%83%AA%E3%83%BC%E5%8C%96
など参考
最近は「関数が第一級のオブジェクト」、要は「手続きの固まり」を文字列や数字のようにモノ扱いできる、RubyやJavaScriptのような言語が普及しているが、そういう言語では「カリー化」の手法が使える。
例えばRubyで、x個の、yから始まる数の総和を求めるorigという関数を考える。

1
2
3
4
5
6
7
def orig&#40;x, y&#41;
  ret = 0
  for i in y..&#40;x + y - 1&#41; do
    ret += i
  end
  return ret
end

（※）
このorig(x, y)を元に、「3個」「4個」「5個」の、yから始まる数の総和を求める関数を作りたい。下の如し。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
def curried_orig&#40;x&#41;
  lambda do &#124;y&#124;
    orig&#40;x, y&#41;
  end
end
&#160;
sum3 = curried_orig&#40;3&#41;
#=&#62; #&#60;Proc:0x0000002a955cf098&#62;
sum4 = curried_orig&#40;4&#41;
#=&#62; #&#60;Proc:0x0000002a955cf098&#62;
sum5 = curried_orig&#40;5&#41;
#=&#62; #&#60;Proc:0x0000002a955cf098&#62;
sum3&#91;10&#93;
#=&#62; 10+11+12 = 33
sum4&#91;10&#93;
#=&#62; 10+11+12+13 = [...]]]></description>
			<content:encoded><![CDATA[<p>カリー化とは</p>
<blockquote><p>複数の引数をとる関数を、引数が「もとの関数の最初の引数」で戻り値が「もとの関数の残りの引数を取り結果を返す関数」であるような関数にすること。</p></blockquote>
<p><a href="http://wiki.onakasuita.org/pukiwiki/?%E3%82%AB%E3%83%AA%E3%83%BC%E5%8C%96"><br />
http://wiki.onakasuita.org/pukiwiki/?%E3%82%AB%E3%83%AA%E3%83%BC%E5%8C%96</a><br />
<a href="http://ja.wikipedia.org/wiki/%E3%82%AB%E3%83%AA%E3%83%BC%E5%8C%96">http://ja.wikipedia.org/wiki/%E3%82%AB%E3%83%AA%E3%83%BC%E5%8C%96</a><br />
など参考</p>
<p>最近は「関数が第一級のオブジェクト」、要は「手続きの固まり」を文字列や数字のようにモノ扱いできる、RubyやJavaScriptのような言語が普及しているが、そういう言語では「カリー化」の手法が使える。</p>
<p>例えばRubyで、x個の、yから始まる数の総和を求めるorigという関数を考える。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> orig<span style="color:#006600; font-weight:bold;">&#40;</span>x, y<span style="color:#006600; font-weight:bold;">&#41;</span>
  ret = <span style="color:#006666;">0</span>
  <span style="color:#9966CC; font-weight:bold;">for</span> i <span style="color:#9966CC; font-weight:bold;">in</span> y..<span style="color:#006600; font-weight:bold;">&#40;</span>x <span style="color:#006600; font-weight:bold;">+</span> y <span style="color:#006600; font-weight:bold;">-</span> <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    ret <span style="color:#006600; font-weight:bold;">+</span>= i
  <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#0000FF; font-weight:bold;">return</span> ret
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p><a href="#foot-0113">（※）</a></p>
<p>このorig(x, y)を元に、「3個」「4個」「5個」の、yから始まる数の総和を求める関数を作りたい。下の如し。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> curried_orig<span style="color:#006600; font-weight:bold;">&#40;</span>x<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#CC0066; font-weight:bold;">lambda</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>y<span style="color:#006600; font-weight:bold;">|</span>
    orig<span style="color:#006600; font-weight:bold;">&#40;</span>x, y<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
sum3 = curried_orig<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">3</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#008000; font-style:italic;">#=&gt; #&lt;Proc:0x0000002a955cf098&gt;</span>
sum4 = curried_orig<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">4</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#008000; font-style:italic;">#=&gt; #&lt;Proc:0x0000002a955cf098&gt;</span>
sum5 = curried_orig<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">5</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#008000; font-style:italic;">#=&gt; #&lt;Proc:0x0000002a955cf098&gt;</span>
sum3<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">10</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#008000; font-style:italic;">#=&gt; 10+11+12 = 33</span>
sum4<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">10</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#008000; font-style:italic;">#=&gt; 10+11+12+13 = 46</span>
sum5<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">10</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#008000; font-style:italic;">#=&gt; 10+11+12+13+14 = 60</span></pre></td></tr></table></div>

<p>sum3やsum4に束縛されるのは、実際にはメソッドというよりProcオブジェクトなので、()ではなく[]で呼び出す。</p>
<p>Ruby1.9.1からは「<code>Proc#curry</code>」が導入され、よりすっきり書ける。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">sum_x = <span style="color:#CC0066; font-weight:bold;">lambda</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x, y<span style="color:#006600; font-weight:bold;">|</span>
  ret = <span style="color:#006666;">0</span>
  <span style="color:#9966CC; font-weight:bold;">for</span> i <span style="color:#9966CC; font-weight:bold;">in</span> x...<span style="color:#006600; font-weight:bold;">&#40;</span>x <span style="color:#006600; font-weight:bold;">+</span> y<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    ret <span style="color:#006600; font-weight:bold;">+</span>= i
  <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#0000FF; font-weight:bold;">return</span> ret
<span style="color:#006600; font-weight:bold;">&#125;</span>
sum3 = sum_x.<span style="color:#9900CC;">curry</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">3</span><span style="color:#006600; font-weight:bold;">&#93;</span>
sum3<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">10</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#008000; font-style:italic;">#=&gt; 10+11+12 = 33</span></pre></td></tr></table></div>

<p>数学的な概念は良く分かっていないけど。<br />
<a href="http://www.nslabs.jp/closure.rhtml"><br />
http://www.nslabs.jp/closure.rhtml</a></p>
<blockquote><p>「カリー化」とは (A, B) -> C という関数をA -> (B -> C) という関数に変換することです。 </p></blockquote>
<p><a href="http://www.khelll.com/blog/ruby/ruby-currying/"><br />
http://www.khelll.com/blog/ruby/ruby-currying/</a></p>
<blockquote><p>Check this function f which takes 3 params x,y,z</p>
<blockquote><p>
f(x,y,z) = 4*x+3*y+2*z</p></blockquote>
<p>Currying means that we can rewrite the function as a composition of 3 functions(a function for each param):</p>
<blockquote><p>f(x)(y)(z) = 2*z+(3*y+(4*x))</p></blockquote>
</blockquote>
<p><a href="http://www.ruby-forum.com/topic/142699#633354">http://www.ruby-forum.com/topic/142699#633354</a></p>
<blockquote><p>
It&#8217;s not difficult at all,</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#CC0066; font-weight:bold;">proc</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x, y, z<span style="color:#006600; font-weight:bold;">|</span> x <span style="color:#006600; font-weight:bold;">+</span> y <span style="color:#006600; font-weight:bold;">+</span> z <span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">curry</span></pre></td></tr></table></div>

<p>returns the proc object equivalent to</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#CC0066; font-weight:bold;">proc</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC0066; font-weight:bold;">proc</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>y<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC0066; font-weight:bold;">proc</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>z<span style="color:#006600; font-weight:bold;">|</span> x <span style="color:#006600; font-weight:bold;">+</span> y <span style="color:#006600; font-weight:bold;">+</span> z <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">&#125;</span></pre></td></tr></table></div>

</blockquote>
<p>実際利用する時に「関数の部分適用」と「カリー化」を区別する意味ってあまり無いかもしれないんだけど。「<code>Proc#curry</code>」はこういう意味ですよ、というのは認識したほうがいいのかも。</p>
<p>その他のカリー化の使い方例　参考：「Fun with Procs in Ruby 1.9」<br />
<a href="http://pragdave.blogs.pragprog.com/pragdave/2008/09/fun-with-procs.html">http://pragdave.blogs.pragprog.com/pragdave/2008/09/fun-with-procs.html</a></p>
<h3 id="foot-0113">＊　＊　＊</h3>
<p><strong>※：</strong>　個人的に普段はこう書いてしまい俺気持ちが悪い。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> orig<span style="color:#006600; font-weight:bold;">&#40;</span>x, y<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#006600; font-weight:bold;">&#40;</span>0...<span style="color:#9900CC;">x</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_a</span>.<span style="color:#9900CC;">map</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>v<span style="color:#006600; font-weight:bold;">|</span> y <span style="color:#006600; font-weight:bold;">+</span> v<span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>d, s<span style="color:#006600; font-weight:bold;">|</span> d <span style="color:#006600; font-weight:bold;">+</span>= s<span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.udzura.jp/2010/01/14/my-own-thought-about-currying/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://blog.udzura.jp/2010/01/14/my-own-thought-about-currying/" />
	</item>
	</channel>
</rss>
