7/19 OoO 第三回 : EGSR08 祭り
イベントのお知らせです。
EGSR 2008の採択論文が出揃ってきたのを受けて、 OoO(=オフラインレンダラ野郎のためのオフラインレンダラについて議論するオフライン会)の第三回が開催されます。
オフラインレンダラ野朗の皆様は奮ってご参加ください。
イベントのお知らせです。
EGSR 2008の採択論文が出揃ってきたのを受けて、 OoO(=オフラインレンダラ野郎のためのオフラインレンダラについて議論するオフライン会)の第三回が開催されます。
オフラインレンダラ野朗の皆様は奮ってご参加ください。
Kagemushaを使って、スコープを汚さないDSLスタイルのinitialize
1 require 'rubygems' 2 require 'kagemusha' 3 4 class Foo 5 def initialize(&block) 6 bar = nil # default value 7 meta = eval('class << self; self end', block.binding) 8 Kagemusha.new(meta) do |kage| 9 kage.def(:bar){|bar|} # substituti on 10 end.swap(&block) 11 @bar = bar 12 end 13 end 14 15 def bar(x); puts x end 16 17 foo = Foo.new do 18 bar 'bar' 19 end 20 21 puts foo.inspect # => #<Foo:0xb7c0 3de0 @bar="bar"> 22 bar "hello!" # => hello!
bindingのコンテクストからメタクラスを取得し、
Kagemushaを使って限定的にbarメソッドを置き換えています。
Foo.newのブロック内から抜けると、
barの呼び出しはグローバルスコープのbarを呼んで、
"hello!"が出力されます。
この方法は、initialize以外にも、
ブロックを受けるインスタンスメソッド全般に利用できます。
例えば、以下のようなコードで、一時変数t
の存在が気になる場合などですね。
1 create_table do |t| 2 t.string :foo 3 end
autotestでall_good時にrerun_all_handle_resメソッドの実装の変化を、
なるべく吸収できるような書き方に変えました。
~/.autotesに書いておくと良いかもしれません。
1 class Autotest 2 alias_method :handle_res ults_old, :handle_res ults 3 undef_meth od :handle_res ults 4 5 def handle_res ults(*args, &block) 6 handle_res ults_old(*args, &block) 7 self.tainted = !self.files_to_t est.empty? 8 end 9 end
ちょっと前にこんな記事(モバイル端末でプログラムを書ける日は来るか)を書いていたのですが、 iphoneの日本語入力のインターフェイスについて説明してる動画を見て、 可能性を感じました。
1方向へのスライドだけでは難しそうだけど、各種コマンドを ↑→↓→みたいに指を離すまでの ストロークにマップしたら相当便利になりそう。 iphoneにおけるVim的な何かが生まれる気がした。
tridentは、
tail -f log/develoと autotest、script/con
を一枚の端末上で同時に実行するRailsプラグインでした(trident: tail -f, autotest and script/con
ほぼ全てのRailsプロダクトに入れていたのですが、 毎回プラグインインストールするのが面倒になってきたので、 Gem化してGitHub上でリリースしました。
使い方ですが、RAILS_ROOT
1 % trident
すると色々立ち上がって script/con
1 > Trident.reload
終了する時はexitでOKです。
See Also
現在使っているConnection
1 ActiveRecord::Base.connection .native_dat abase_type s.keys 2 => [:boolean, :date, :binary, :datetime, :string, :integer, :decimal, :primary_ke y, :time, :float, :timestamp, :text]
ブラックボックスなシステムを間に挟んで caller と callee 間で通信を行なう Wormholeのバージョン0.1.2 をリリースしました。
新しい機能:
1 w = Wormhole.catch do 2 end #=> Wormhole::LateReturn Error 3 (snip) 4 w.return 5 6 Worrmhole.catch do 7 end.return # => OK
ついでにWormholeの使用例を。
app/contro
1 require 'wormhole' 2 class FooController < Applicatio nControlle r 3 def index 4 Wormhole.catch do 5 render :action => 'index' 6 end.return do |result| 7 result[:html] = "Hello, world!" 8 end 9 end 10 end
app/views/
1 <% Wormhole.throw do |result| %> 2 <%= result[:html] #=> 'Hello, world!' %> 3 <% end %>
Viewのコードから一端Controller
このように、おいそれとは変更できないようなフレームワークのコードを跨いで、呼び出し側と呼び出し元の間で情報のやり取りを行なう事ができます。
D言語やPythonのように、複素数リテラルが欲しかったのですが、 無かったのでそれっぽい動きをさせるようにNumeric#iを定義してみました。
numeric_i.
1 require 'complex' 2 3 class Numeric 4 def i 5 self * Complex::I 6 end 7 end
これだけです。あとは以下のようにして使います。
1 require 'numeric_i' 2 3 1 + 1.i #=> Complex(1, 1) 4 (Math::E ** (Math::PI.i)).real #=> -1.0
(自分が)簡単に使えるように、GitHubでGemを作っておきました。 利用する場合は以下の手順で導入できます。
1 gem sources -a http://gems.github.c om 2 sudo gem install genki-nume ric_i
名前空間のお陰で、シンプルなGemも気軽に公開できて良い時代ですね。
See Also
(追記) スキーマのネームスペースが正しくありませんでした。修正しました。最新のネームスペースは適宜参照してください。
検索エンジンに正しくインデックスしてもらうのは大事です。今回モバイルのも同時に作ってみました。
下記Googleのウェブマスター向けヘルプセンターを参照。
1 map.m_sitemap 'mobile_sitemap.xml', :controller => "mobile", :action => "sitemap" 2 map.sitemap 'sitemap.xm l', :controller => "top", :action => "sitemap"
ファイルを作成します。:app/views/
(注) サイトマップのスキーマが0.9だと、Google,Yahoo, MSNなどのサーチエンジンが読めるようです。
(注) lastmodタグについてはDate.today
1 # sitemap.xml 2 xml.instruct! :xml, :version=> '1.0', :encoding => 'UTF-8' 3 xml.urlset :xmlns => 'http://www .sitemaps. org/schema s/sitemap/ 0.9' do 4 xml.url do 5 xml.loc root_url 6 xml.changefreq 'daily' 7 xml.priority '0.9' 8 end 9 10 xml.url do 11 xml.loc help_url 12 xml.changefreq 'monthly' 13 xml.priority '0.9' 14 end 15 end
1 # mobile_sitemap.xml 2 xml.instruct! :xml, :version=> '1.0', :encoding => 'UTF-8' 3 xml.urlset :xmlns => 'http://www .google.co m/schemas/ sitemap/0. 84', 4 "xmlns:mobi le" => 'http://www .google.co m/schemas/ sitemap-mo bile/1.0' do 5 xml.url do 6 xml.loc m_url 7 xml.lastmod lastmodifi ed.xmlschema 8 xml.mobile :mobile 9 end
HTMLに下記を書いておくと、Google, Yahoo!, MSNもとっていってくれます。
1 <link rel="alternate" type="application/rss+xml" title="your title" href="sitemap.xm l" />
http://bra
http://www
http://www
http://blo
今のコミット間違った―!というときに、コミット内容もなしにして戻すのがgit-reset --hard。
git reset --hard HEAD~3
Rewind the master branch to get rid of those three commits.
http://www.kernel.or g/pub/soft ware/scm/g it/docs/gi t-reset.ht ml
一つ前のコミットまで戻る時は、HEAD^を引数に渡すと戻ってくれます。 あーよかった!