<?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/"
	>

<channel>
	<title>ハセテツラボ &#187; Ruby</title>
	<atom:link href="http://tt-house.com/category/ruby/feed" rel="self" type="application/rss+xml" />
	<link>http://tt-house.com</link>
	<description>生涯一開発屋</description>
	<lastBuildDate>Fri, 03 Sep 2010 14:01:41 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>RubyonRailsでOAuth経由でTwitterのタイムラインを取得する</title>
		<link>http://tt-house.com/2010/06/rubyonrails-oauth-twitter.html</link>
		<comments>http://tt-house.com/2010/06/rubyonrails-oauth-twitter.html#comments</comments>
		<pubDate>Fri, 18 Jun 2010 03:56:23 +0000</pubDate>
		<dc:creator>ハセテツ</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[RubyOnRails]]></category>

		<guid isPermaLink="false">http://tt-house.com/?p=301</guid>
		<description><![CDATA[OAuthを使えるようになっておけば今後いろいろなマッシュアップに便利だろうと思い、試してみました。RoRで書いたのは、最近ご無沙汰過ぎてあまりに覚えてなかったのでリハビリを兼ねたわけです。 class TwitterAuth require &#8216;oauth&#8217; def self.consumer OAuth::Consumer.new( [Consumer key], [Consumer secret], :site =&#62; &#8220;http://api.twitter.com&#8221; ) end def self.request_token(_token, _secret) OAuth::RequestToken.new( consumer, _token, _secret ) end def self.access_token(_token, _secret) OAuth::AccessToken.new( consumer, _token, _secret ) end end まずは上記のクラスを作成。OAuth関連の処理はこっちにやらせます。で、次がコントローラー。 require &#8216;rubytter&#8217; class AuthController &#60; ApplicationController def index request_token = TwitterAuth.consumer.get_request_token(:oauth_callback =&#62; &#8220;http://#{request.host_with_port}/auth/callback&#8221;) session[:request_token] = request_token redirect_to request_token.authorize_url [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_blue" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Ftt-house.com%252F2010%252F06%252Frubyonrails-oauth-twitter.html%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22RubyonRails%E3%81%A7OAuth%E7%B5%8C%E7%94%B1%E3%81%A7Twitter%E3%81%AE%E3%82%BF%E3%82%A4%E3%83%A0%E3%83%A9%E3%82%A4%E3%83%B3%E3%82%92%E5%8F%96%E5%BE%97%E3%81%99%E3%82%8B%22%20%7D);"></div>
<p>OAuthを使えるようになっておけば今後いろいろなマッシュアップに便利だろうと思い、試してみました。RoRで書いたのは、最近ご無沙汰過ぎてあまりに覚えてなかったのでリハビリを兼ねたわけです。</p>
<blockquote><p>class TwitterAuth<br />
require &#8216;oauth&#8217;</p>
<p style="padding-left: 30px;">def self.consumer</p>
<p style="padding-left: 60px;">OAuth::Consumer.new(<br />
[Consumer key],<br />
[Consumer secret],<br />
:site =&gt; &#8220;http://api.twitter.com&#8221;<br />
)</p>
<p style="padding-left: 30px;">end</p>
<p style="padding-left: 30px;">def self.request_token(_token, _secret)</p>
<p style="padding-left: 60px;">OAuth::RequestToken.new(<br />
consumer,<br />
_token,<br />
_secret<br />
)</p>
<p style="padding-left: 30px;">end</p>
<p style="padding-left: 30px;">def self.access_token(_token, _secret)</p>
<p style="padding-left: 60px;">OAuth::AccessToken.new(<br />
consumer,<br />
_token,<br />
_secret<br />
)</p>
<p style="padding-left: 30px;">end</p>
<p>end</p></blockquote>
<p>まずは上記のクラスを作成。OAuth関連の処理はこっちにやらせます。で、次がコントローラー。</p>
<blockquote><p>require &#8216;rubytter&#8217;</p>
<p>class AuthController &lt; ApplicationController</p>
<p style="padding-left: 30px;">def index</p>
<p style="padding-left: 60px;">request_token = TwitterAuth.consumer.get_request_token(:oauth_callback =&gt; &#8220;http://#{request.host_with_port}/auth/callback&#8221;)<br />
session[:request_token] = request_token<br />
redirect_to request_token.authorize_url</p>
<p style="padding-left: 30px;">end</p>
<p style="padding-left: 30px;">def callback</p>
<p style="padding-left: 60px;">_token = session[:request_token]<br />
request_token = TwitterAuth.request_token(_token.token, _token.secret)<br />
access_token = request_token.get_access_token(<br />
{},<br />
:oauth_token =&gt; params[:oauth_token],<br />
:oauth_verifier =&gt; params[:oauth_verifier]<br />
)<br />
session[:request_token]=nil<br />
_account = {<br />
:user_id =&gt; access_token.params[:user_id],<br />
:token =&gt; access_token.token,<br />
:secret =&gt; access_token.secret<br />
}<br />
session[:account] = _account<br />
redirect_to :action =&gt; &#8220;timeline&#8221;</p>
<p style="padding-left: 30px;">end</p>
<p style="padding-left: 30px;">def timeline</p>
<p style="padding-left: 60px;">_account = session[:account]<br />
token = TwitterAuth.access_token(_account[:token] , _account[:secret])<br />
_twitter = OAuthRubytter.new(token)<br />
@user_timeline = _twitter.user_timeline(_account[:user_id],:count =&gt; 100)</p>
<p style="padding-left: 30px;">end</p>
<p>end</p></blockquote>
<p>def timelineが自分のタイムラインを取ってくるところで、「:count=&gt;100」と指定しているのは「新着100件取ってきて」っていうことです。ここを指定しないでおくと規定値では２０件取得してくれます。def callbackでセッションに入れたuser_id、token、secretをデータベース等に保存しておけば次回以降はこれらだけで認証とツイートの取得ができます（というか認証自体はtokenとsecretだけでOK）。</p>
<p><a title="アプリケーション登録申請" href="http://twitter.com/apps/new" target="_blank">Twitterのアプリケーション登録申請</a>で登録する際、コールバックURLを指定しないとWebアプリケーションとして登録されません。「開発中だからURLなんて決まってないよ」っていうケースでも、適当なURLを登録しておいてください。どうせ実際のコールバックURLはRubyのスクリプトで指定しなおしています。ハセテツはここに気が付かず、そこそこの時間を浪費しました。</p>
<p>で、タイムラインを表示するviewは</p>
<blockquote><p>&lt;ul&gt;<br />
&lt;% for item in @user_timeline do -%&gt;<br />
&lt;li&gt;&lt;%= item.text %&gt;&lt;/li&gt;<br />
&lt;% end -%&gt;<br />
&lt;/ul&gt;</p></blockquote>
<p>こんな感じです。</p>

]]></content:encoded>
			<wfw:commentRss>http://tt-house.com/2010/06/rubyonrails-oauth-twitter.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHPとRubyとPythonのパフォーマンスを比較してみました</title>
		<link>http://tt-house.com/2010/05/php-ruby-python-2.html</link>
		<comments>http://tt-house.com/2010/05/php-ruby-python-2.html#comments</comments>
		<pubDate>Tue, 11 May 2010 02:44:50 +0000</pubDate>
		<dc:creator>ハセテツ</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://tt-house.com/?p=286</guid>
		<description><![CDATA[同じような処理をさせた場合、PHP、Ruby、Pythonでどの言語が一番早いのか。非常に気になるところでありますが、試したことがありませんでした。今回試してみたところ、意外な結果が出たのです。]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_blue" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Ftt-house.com%252F2010%252F05%252Fphp-ruby-python-2.html%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22PHP%E3%81%A8Ruby%E3%81%A8Python%E3%81%AE%E3%83%91%E3%83%95%E3%82%A9%E3%83%BC%E3%83%9E%E3%83%B3%E3%82%B9%E3%82%92%E6%AF%94%E8%BC%83%E3%81%97%E3%81%A6%E3%81%BF%E3%81%BE%E3%81%97%E3%81%9F%22%20%7D);"></div>
<p><a title="RubyとPythonについて考えてみました" href="http://tt-house.com/2010/04/think-about-ruby-python.html" target="_blank">前回のエントリ</a>が結局フレームワークの話になってしまったため、新しいエントリを書きました。</p>
<p>同じような処理をさせた場合、どの言語が一番早いのか。非常に気になるところでありますが、試したことがありませんでした。今回試してみたところ、意外な結果が出たのです。</p>
<p>下の方に書いたそれぞれのプログラムを実行した結果の、プログラム内部で計測したミリ秒を比較してます。</p>
<table class="post">
<tbody>
<tr>
<th>PHP</th>
<th>Ruby</th>
<th>Python</th>
</tr>
<tr>
<td>2.80秒</td>
<td>0.36秒</td>
<td>0.93秒</td>
</tr>
</tbody>
</table>
<p>3回計測した平均です。同じ処理をさせるプログラムを書いたつもりですが、本当にフェアなものになっているのか正直自信がありません。そもそもPHPはWebアプリを主な目的としているとのことなので、こういう処理は得意としていないのかもしれない。</p>
<p>また、それぞれのプログラムのループ内2行目、文字列連結の部分をコメントアウトして実行してみると、これまた意外な結果が出ました。</p>
<table class="post">
<tbody>
<tr>
<th>PHP</th>
<th>Ruby</th>
<th>Python</th>
</tr>
<tr>
<td>0.07秒</td>
<td>0.06秒</td>
<td>0.39秒</td>
</tr>
</tbody>
</table>
<p>PHPは文字列連結が苦手なんだろうか。暗黙的な型変換にコストがかかるのだろうか、等々考えてしまいます。それとも秒を求める部分がネックなんですかね。その辺は今後追っていきます。</p>
<p>ただ、これまでPythonの方がRubyより早い、と思っていたために自分の無知さに猛烈に恥ずかしい思いをしています。</p>
<p>実際にMySQLと連携するバッチを書いたときにはRubyの方が遅かったり、Railsアプリがもっさりだったり、「速い」という印象がまったくありませんでした。が、それはある特定の面しかみていない上での思い込みだったのかもしれません。</p>
<p>これはRailsが遅いのか、もしくはMySQL/Rubyが遅いのか。当時の書き方がマズかったのか。今後、いろんなケースでこういった比較をしてみようかと。</p>
<p>今回の検証がすべてではないし、これが答えだとも思っていません。また、そういう意図を含んだエントリではありません。今回の検証だって、特定の面しか見ていません。が、あまりに意外な結果だったため、自戒の念を含めてブログに書きました。</p>
<p>「こう書くともっとフェアだよ」「こういうテストの方がいいんじゃない」等、アドバイスいただけるとうれしいです。</p>
<p>フレームワークまで含めた検証した方が現実的なのだろうか。。。</p>
<hr /><strong>PHP</strong></p>
<blockquote><p>#!/usr/bin/php<br />
&lt;?<br />
function microtime_float()<br />
{</p>
<p style="padding-left: 30px;">list($usec, $sec) = explode(&#8221; &#8220;, microtime());<br />
return ((float)$usec + (float)$sec);</p>
<p>}<br />
$_start = microtime_float();<br />
$_sec = date(&#8216;s&#8217;);</p>
<p>for($i=1;$i&lt;100000;$i++)<br />
{</p>
<p style="padding-left: 30px;">$_val = (mt_rand(0, 100) + $i) / $i;<br />
$_str = $_val.date(&#8216;s&#8217;);</p>
<p>}</p>
<p>echo microtime_float() &#8211; $_start;<br />
?&gt;</p></blockquote>
<p><strong>Ruby</strong></p>
<blockquote><p>#! /usr/local/bin/ruby</p>
<p>i=1<br />
_start = Time.now</p>
<p>99999.times do</p>
<p style="padding-left: 30px;">_val = rand(100) / i<br />
_str = _val.to_s + Time.now.sec.to_s<br />
i += 1</p>
<p>end</p>
<p>p (Time.now &#8211; _start).to_f</p></blockquote>
<p><strong>Python</strong></p>
<blockquote><p>#!/usr/bin/python<br />
# -*- coding: utf-8 -*-<br />
import datetime<br />
import random</p>
<p>_start = datetime.datetime.now()</p>
<p>for i in range(1, 100000):</p>
<p style="padding-left: 30px;">_val = random.randint(0, 100) / i<br />
_str = str(_val) + str(datetime.datetime.now().second)</p>
<p>print datetime.datetime.now() &#8211; _start</p></blockquote>
<hr />検証環境</p>
<p>CPU:Xeon 1.86GHz<br />
メモリ:2GB<br />
OS: CentOS5.4 32bit<br />
PHP:5.2.11<br />
Ruby:1.8.7<br />
Python:2.5.2</p>

]]></content:encoded>
			<wfw:commentRss>http://tt-house.com/2010/05/php-ruby-python-2.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>RubyとPythonについて考えてみました</title>
		<link>http://tt-house.com/2010/04/think-about-ruby-python.html</link>
		<comments>http://tt-house.com/2010/04/think-about-ruby-python.html#comments</comments>
		<pubDate>Tue, 27 Apr 2010 05:28:06 +0000</pubDate>
		<dc:creator>ハセテツ</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[RubyOnRails]]></category>

		<guid isPermaLink="false">http://tt-house.com/?p=278</guid>
		<description><![CDATA[ハセテツは複数の言語を状況に応じて使い分けてきましたが、最近のWeb系開発はPython（+Django）がメインになってきています。

ちょっと前までRuby（+Rails）だったのですが、いろいろと考えるところもあり、乗り換えました。その乗り換えた理由をまとめてみようと思います。

まとめてみたら「やっぱりRailsじゃね？」となるかもしれません。ｗ]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_blue" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Ftt-house.com%252F2010%252F04%252Fthink-about-ruby-python.html%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Ruby%E3%81%A8Python%E3%81%AB%E3%81%A4%E3%81%84%E3%81%A6%E8%80%83%E3%81%88%E3%81%A6%E3%81%BF%E3%81%BE%E3%81%97%E3%81%9F%22%20%7D);"></div>
<p>ハセテツは複数の言語を状況に応じて使い分けてきましたが、最近のWeb系開発はPython（+Django）がメインになってきています。</p>
<p>ちょっと前までRuby（+Rails）だったのですが、いろいろと考えるところもあり、乗り換えました。その乗り換えた理由をまとめてみようと思います。</p>
<p>まとめてみたら「やっぱりRailsじゃね？」となるかもしれません。ｗ</p>
<p>Ruby（+Rails）</p>
<ul>
<li>Rails便利すぎる。</li>
<li>コントローラとモデルが別々のファイルになってくれるのはソースが追いやすい。</li>
<li>Railsを通さない画像やCSSはpublicフォルダに置けばよいのはわかりやすい。</li>
<li>urlディスパッチャーがいまいち使いにくい。（知らないだけかも）</li>
<li>APサーバはmongrel一択？</li>
<li>VirtualHost使おうとするとApache+Passenger（mod_rails）だが、これが重い。（チューニングで速くなるのかも）</li>
<li>というか、Railsがそもそも重い。</li>
<li>ちゅーか、Rubyが重い、遅い。</li>
<li>PassengerはWindowsじゃ動かない。</li>
<li>mongrelもPassengerより激速軽快かというと、そうでもない。</li>
<li>gem便利すぎ。</li>
<li>ワンライナで書く人が多くて、Perlとおなじ匂いがする。</li>
</ul>
<p>ハセテツが使っていたRubyは1.8で、速くなったといわれる1.9には触れていないのでもしかすると古いのかもしれません。でも、Railsって1.9には対応してないですよね？（本日現在）</p>
<p>Python（+Django）</p>
<ul>
<li>Django便利すぎる。</li>
<li>日本語の書籍、情報が少なすぎる。（ハマるとキツい）</li>
<li>modelが一枚のファイルなので、大量のmodelがあると可読性が激しくダウン。</li>
<li>viewファイルをフォルダで分けたくても、アプリケーションフォルダ直下以外にviewファイル置くなら「sys.path.append」しないといけない。（違う？）</li>
<li>urlディスパッチャーのカスタマイズが超便利。</li>
<li>mod_pythonが使えるので、Windows環境だろうがLinux環境だろうが気にせずVirtualHostが使える。</li>
<li>速い。</li>
<li>日本語大変。Shift_JIS怖い。</li>
<li>easy_installでパッケージのインストールは楽だが、管理が悪夢。</li>
</ul>
<p>メリットとデメリットが入り乱れました。見難くてすいません。</p>
<p>結局フルスタックのフレームワークということで、DjangoとRailｓに関しては一長一短だと思います。個人的にはRailsの方が良くできてたかなぁと。</p>
<p>最終的に、「RubyonRailsは重い」というオチになってしまうのです。Rubyも、Railsも、です。ただ、これはRuby1.9が実は劇的に速くなっていて、1.9に対応したRailsがリリースされたらすべて解決されるのかもしれません。</p>
<p>それでも、いまのところPythonの軽快さ、シンプルさに満足しています。あと、GAEでPythonが動くのもいいですよね。</p>
<p>「Googleが導入した言語」というのがハセテツ的に琴線に触れたのかもしれません。</p>
<p>ミーハーですいません。ｗ</p>

]]></content:encoded>
			<wfw:commentRss>http://tt-house.com/2010/04/think-about-ruby-python.html/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Rubyで文字列エンコードの変換</title>
		<link>http://tt-house.com/2009/08/encoding-on-ruby.html</link>
		<comments>http://tt-house.com/2009/08/encoding-on-ruby.html#comments</comments>
		<pubDate>Thu, 13 Aug 2009 15:43:23 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.tt-house.com//wp/?p=107</guid>
		<description><![CDATA[もう全部UTF8にしたいです。 require 'kconv' StrUTF8&#038;...]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_blue" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Ftt-house.com%252F2009%252F08%252Fencoding-on-ruby.html%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Ruby%E3%81%A7%E6%96%87%E5%AD%97%E5%88%97%E3%82%A8%E3%83%B3%E3%82%B3%E3%83%BC%E3%83%89%E3%81%AE%E5%A4%89%E6%8F%9B%22%20%7D);"></div>
<p>もう全部UTF8にしたいです。</p>
<blockquote style="margin-right: 0px;" dir="ltr"><p>require &#8216;kconv&#8217;</p>
<p>StrUTF8   = 文字列.toutf8<br />
StrShiftJIS = 文字列.tosjis<br />
StrEUC   = 文字列.toeuc</p></blockquote>
<p dir="ltr">見たとおり、上からUTF8に変換、Shift_JISに変換、EUCに変換、です。</p>

]]></content:encoded>
			<wfw:commentRss>http://tt-house.com/2009/08/encoding-on-ruby.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>RubyでPOST送信して結果を受け取る</title>
		<link>http://tt-house.com/2009/06/ruby-post-data.html</link>
		<comments>http://tt-house.com/2009/06/ruby-post-data.html#comments</comments>
		<pubDate>Wed, 17 Jun 2009 11:55:46 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[RubyOnRails]]></category>

		<guid isPermaLink="false">http://www.tt-house.com//wp/?p=101</guid>
		<description><![CDATA[RubyでHTTP経由でのXMLの受信と解析でGETする方法は書きましたが、PO...]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_blue" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Ftt-house.com%252F2009%252F06%252Fruby-post-data.html%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Ruby%E3%81%A7POST%E9%80%81%E4%BF%A1%E3%81%97%E3%81%A6%E7%B5%90%E6%9E%9C%E3%82%92%E5%8F%97%E3%81%91%E5%8F%96%E3%82%8B%22%20%7D);"></div>
<p><a href="http://www.tt-house.com/2009/05/rubyhttpxml.html">RubyでHTTP経由でのXMLの受信と解析</a>でGETする方法は書きましたが、POSTまでは書いていませんでした。今回はRubyでPOSTする方法です。GETで猛烈に長いクエリをつければPOSTできなくても同じことが実現できますが、まぁそこは気にせず。</p>
<blockquote style="MARGIN-RIGHT: 0px" dir="ltr">
<p>require &#8216;net/http&#8217;<br />Net::HTTP.version_1_2<br />http = Net::HTTP.new(ホスト名, 80)<br />response = http.post( &#8216;/hoge.php&#8217;, &#8216;a=hoge&amp;b=hogehoge&#8217; )<br />p response.body</p>
</blockquote>
<p>これだけ。ホスト名にはhttpは付けません。ポート番号は省略しても大丈夫です。Content-Type等は必要に応じて。newするときの第三引数です。「application/x-www-form-urlencoded」でいいのかな？ハセテツはつけてませんが、きちんとPOSTできてます。あー、相手がUTF8じゃない場合とかは文字コードの指定が必要かも。</p>
<p>responseの中身を見ればHTTPステータスコードとかも入っていると思います。その辺は割愛。</p>
<p>次はファイルのアップロードのやり方なんかも調べてみましょう。使うかどうかは微妙な気もしますが。</p>

]]></content:encoded>
			<wfw:commentRss>http://tt-house.com/2009/06/ruby-post-data.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>RubyでHTTP経由でのXMLの受信と解析</title>
		<link>http://tt-house.com/2009/05/rubyhttpxml.html</link>
		<comments>http://tt-house.com/2009/05/rubyhttpxml.html#comments</comments>
		<pubDate>Wed, 27 May 2009 20:18:51 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[RubyOnRails]]></category>

		<guid isPermaLink="false">http://www.tt-house.com//wp/?p=98</guid>
		<description><![CDATA[まぁ簡単にいうとRSSリーダーみたいなことをしたいときに使います。相手がRSSじ...]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_blue" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Ftt-house.com%252F2009%252F05%252Frubyhttpxml.html%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Ruby%E3%81%A7HTTP%E7%B5%8C%E7%94%B1%E3%81%A7%E3%81%AEXML%E3%81%AE%E5%8F%97%E4%BF%A1%E3%81%A8%E8%A7%A3%E6%9E%90%22%20%7D);"></div>
<p>まぁ簡単にいうとRSSリーダーみたいなことをしたいときに使います。相手がRSSじゃなくてもOKです。Web上のコンテンツを読み込みたいときに使います。ただ、今回のサンプルだとgetしかできないので、制限はあります。</p>
<blockquote style="MARGIN-RIGHT: 0px" dir="ltr">
<p>require &#8216;open-uri&#8217;</p>
<p>open(&#8220;http://www.tt-house.com/atom.xml&#8221;){|f|<br /><http: ?){|f|<br #{params[:id]} parse mecab tools.alpharise.jp / />&nbsp; data = f.read<br />&nbsp; xmldoc = REXML::Document.new data<br />&nbsp; xmldoc.elements.each(&#8220;feed/entry/title&#8221;){|element|<br />&nbsp;&nbsp;&nbsp; p element.text<br />&nbsp; }<br />}</p>
</blockquote>
<p dir="ltr">文字コードのことは気にしてません。</p>
<p dir="ltr">これでハセテツラボRSSのタイトル一覧が取得できます。他のサービスとのマッシュアップ等にも使えますが、やっぱりデータのpostもできないと使い勝手は悪いですよね。</p>
<p dir="ltr">postをするサンプルは次回にしましょう。</p>

]]></content:encoded>
			<wfw:commentRss>http://tt-house.com/2009/05/rubyhttpxml.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>RubyでZIP圧縮、パスワードも設定する</title>
		<link>http://tt-house.com/2009/03/zip-ruby.html</link>
		<comments>http://tt-house.com/2009/03/zip-ruby.html#comments</comments>
		<pubDate>Fri, 13 Mar 2009 11:07:11 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[RubyOnRails]]></category>

		<guid isPermaLink="false">http://www.tt-house.com//wp/?p=89</guid>
		<description><![CDATA[RubyonRailsアプリケーションでサーバサイドでファイル圧縮、パスワードの...]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_blue" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Ftt-house.com%252F2009%252F03%252Fzip-ruby.html%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Ruby%E3%81%A7ZIP%E5%9C%A7%E7%B8%AE%E3%80%81%E3%83%91%E3%82%B9%E3%83%AF%E3%83%BC%E3%83%89%E3%82%82%E8%A8%AD%E5%AE%9A%E3%81%99%E3%82%8B%22%20%7D);"></div>
<p>RubyonRailsアプリケーションでサーバサイドでファイル圧縮、パスワードの設定をしてダウンロードさせる必要があったので調べてみました。</p>
<p>ZipRubyがあったりRubyZipがあったり、どっちがどうなんだか、相変わらずRubyは混乱します。今回は評判のよさそうなZipRubyを使いました。gemでインストールできて、バージョンは0.2.9でした。バージョン低いのがちと気になります。</p>
<blockquote style="margin-right: 0px;" dir="ltr"><p>gem install zipruby</p></blockquote>
<p>これだけでインストールは終わりです。</p>
<blockquote style="margin-right: 0px;" dir="ltr"><p>require &#8216;rubygems&#8217;<br />
require &#8216;zipruby&#8217;</p>
<p>Zip::Archive.open(&#8216;hoge.zip&#8217;,Zip::CREATE) do |arc|<br />
  arc.add_file(&#8216;test.pdf&#8217;)<br />
end</p>
<p>Zip::Archive.encrypt(&#8216;hoge.zip&#8217;, &#8216;password&#8217;)</p></blockquote>
<p>コレだけです。簡単すぎる。</p>
<p>複数ファイルをひとまとめにしたい場合は</p>
<blockquote style="margin-right: 0px;" dir="ltr"><p>arc.add_file(&#8216;test.pdf&#8217;)</p></blockquote>
<p> </p>
<p>を繰り返せばOK。ただ、フォルダごとごっそり圧縮、という方法は不明。フォルダ名を引数に指定したら怒られてしまいました。当然の結果。</p>
<p>最後の行がパスワードの設定です。ZIPファイル作るときにパスワードを設定するのではなく、既存のZIPファイルに対してパスワードを設定する、というイメージですね。Lhaplusではちゃんと解凍できました。</p>

]]></content:encoded>
			<wfw:commentRss>http://tt-house.com/2009/03/zip-ruby.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>RubyでMySQLに接続</title>
		<link>http://tt-house.com/2009/03/ruby-to-mysql.html</link>
		<comments>http://tt-house.com/2009/03/ruby-to-mysql.html#comments</comments>
		<pubDate>Thu, 12 Mar 2009 11:43:58 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.tt-house.com//wp/?p=88</guid>
		<description><![CDATA[MySQL/Ruby と Ruby/MySQL の二つがあるようです。どっちがど...]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_blue" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Ftt-house.com%252F2009%252F03%252Fruby-to-mysql.html%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Ruby%E3%81%A7MySQL%E3%81%AB%E6%8E%A5%E7%B6%9A%22%20%7D);"></div>
<p>MySQL/Ruby と Ruby/MySQL の二つがあるようです。どっちがどっちか混乱しましたが、MySQL/Rubyがgemでとってこれて、Cで書かれたAPIのようです。Ruby/MySQL はRubyで書かれているようで、比較的低速らしいです。今回はMySQL/Rubyを利用しました。</p>
<p>gem install mysql</p>
<p>で一式インストールできます。PHPのPEARやRubyのgemは、一度つかったらやめられません。これは堕落なのでしょうか。</p>
<p>で、今回はWindows上にMySQL5.0をインストールしてつないでみたのですが、バリバリ文字化けました。DBはUTF8で作ってあって、ソースも以下のようにUTF8であることを明記していました。</p>
<blockquote style="margin-right: 0px;" dir="ltr"><p>require &#8216;rubygems&#8217;<br />
require &#8220;mysql&#8221;<br />
require &#8216;iconv&#8217;</p>
<p>$KCODE = &#8216;UTF-8&#8242;</p>
<p>db = Mysql::connect(&#8220;ホスト名&#8221;, &#8220;ユーザ名&#8221;, &#8220;パスワード&#8221;, &#8220;DB名称&#8221;)<br />
rs = db.query &#8216;クエリ&#8217;<br />
rs.each do |item|<br />
  p item[0]<br />
end</p></blockquote>
<p dir="ltr">なんでなんで？と調べていたら、my.ini（あー、iniってあたりがWindowsだなー、Linuxだとmy.cnfだったかな？）の[mysqld]に</p>
<blockquote style="margin-right: 0px;" dir="ltr">
<p dir="ltr">default-character-set=utf8<br />
skip-character-set-client-handshake</p>
</blockquote>
<p dir="ltr">の追記が必要らしい。これはクライアントからの接続時に「文字コードはUTF8でっせ」とデフォルトで設定してあげるためのものらしいです。「set names utf8」と同じ意味ですね。これで文字化けずにWindows上でRubyとMySQLを利用することが出来るようになりました。</p>
<p dir="ltr">これまではC#とSQL Serverで統計とか集計のプログラム書いてたけど、Rubyの方がラクチンなんですよね。Railsばっかり注目されてますけど、Rubyは便利ですよ。</p>
<p dir="ltr"> </p>

]]></content:encoded>
			<wfw:commentRss>http://tt-house.com/2009/03/ruby-to-mysql.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>RubyでDatetimeオブジェクトをフォーマット</title>
		<link>http://tt-house.com/2008/12/ruby-datetime-format.html</link>
		<comments>http://tt-house.com/2008/12/ruby-datetime-format.html#comments</comments>
		<pubDate>Tue, 30 Dec 2008 03:41:06 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.tt-house.com//wp/?p=79</guid>
		<description><![CDATA[Datetimeオブジェクト.strftime("%Y/%m/%d %H:%M...]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_blue" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Ftt-house.com%252F2008%252F12%252Fruby-datetime-format.html%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Ruby%E3%81%A7Datetime%E3%82%AA%E3%83%96%E3%82%B8%E3%82%A7%E3%82%AF%E3%83%88%E3%82%92%E3%83%95%E3%82%A9%E3%83%BC%E3%83%9E%E3%83%83%E3%83%88%22%20%7D);"></div>
<blockquote style="margin-right: 0px;" dir="ltr"><p>Datetimeオブジェクト.strftime(&#8220;%Y/%m/%d %H:%M&#8221;)</p></blockquote>
<p>上記で、「2008/12/30 3:42」といった感じでyyyy/mm/dd HH:MM形式で出力される。まぁどの言語も大して変わりませんな。</p>

]]></content:encoded>
			<wfw:commentRss>http://tt-house.com/2008/12/ruby-datetime-format.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rubyで文字列を桁埋め（パディング）</title>
		<link>http://tt-house.com/2008/12/ruby-padding.html</link>
		<comments>http://tt-house.com/2008/12/ruby-padding.html#comments</comments>
		<pubDate>Thu, 25 Dec 2008 15:14:23 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.tt-house.com//wp/?p=76</guid>
		<description><![CDATA[p sprintf("%0#{5}d",1) 「00001」と出力されます。p...]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_blue" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Ftt-house.com%252F2008%252F12%252Fruby-padding.html%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Ruby%E3%81%A7%E6%96%87%E5%AD%97%E5%88%97%E3%82%92%E6%A1%81%E5%9F%8B%E3%82%81%EF%BC%88%E3%83%91%E3%83%87%E3%82%A3%E3%83%B3%E3%82%B0%EF%BC%89%22%20%7D);"></div>
<blockquote dir="ltr" style="MARGIN-RIGHT: 0px">
<p>p sprintf(<span class="synSpecial">&#8220;</span><span class="synConstant">%0</span><span class="synSpecial">#{5}</span><span class="synConstant">d</span><span class="synSpecial">&#8220;</span>,1)</p>
</blockquote>
<p>「00001」と出力されます。<br />printfとは違い、文字列オブジェクトが返ってきます。</p>
<p>&nbsp;</p>

]]></content:encoded>
			<wfw:commentRss>http://tt-house.com/2008/12/ruby-padding.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
