Enumerable#grepを使う事で、文字列の配列から、 所望のパターンにマッチするものだけを抜き出す事が出来ますが、 以下のようにブロックを指定する事で、 マッチした部分だけを取り出す事が出来ます。

   1  Dir["config/environments/*.rb"].grep(%r{(\w+)\.rb$}){$1}
   2  #=> ["development", "production", "rake", "staging", "test"]

痒いところに手が届く感じですね。

posted by Png genki on Thu 30 Apr 2009 at 14:32

Sassを使うようになって、属性とタグの区別がつきにくいなと感じていたので、シンタックスハイライトをする方法を探してみました。

これを使うと、こんな感じになります。

ss

だいぶ見やすくなりました。

posted by Png genki on Sun 26 Apr 2009 at 17:34

例えばJavaScriptだと"\u3000"と書けば良かったのですが、少なくともRuby-1.8系ではこれに相当する記法が無さそうだったので、いつも困ってました。 色々調べてみた結果、以下のようにすれば良さそうです。

   1  [0x3000].pack("U") == " "  #=> true

長い事迂回していた問題だったのでスッキリしました :-)

Special Thanks

  • @yukiko pack("U")が使えそうという情報をありがとうございます。
posted by Png genki on Fri 24 Apr 2009 at 06:28

Hello, Merbists!

Today, I explain how to develop Merb apps that runs on GAE/J environment by using dm-datastore-adapter.

First of all, here is whole source code of an example app. Please check it out.

After checking it out, you must edit appengine-web.xml file. Open it by editor and change the application name to yours. And then you should make war directory by typing this command.

   1  MERB_ROOT% jruby -S warble war

It generates files under tmp/war.

So far, you are ready to deploy this app to GAE/J Of course, you need an account of GAE/J to do it. Please get it in advance :-)

Let us go to deploy by this command.

   1  MERB_ROOT% appcfg.sh update -e {youraccount@gmail.com} tmp/war  

This process takes a time for the first time. If the log didn't say any errors, you got success!

Now your first Merb app on GAE/J is here at http://{your app name}.appspot.com/

Congrats!

Further improvement is your home work :-p

APPENDIX

All required gems are being packed into jar file located at lib/merb.jar. This enables you to pass the limit of which you can upload only 1000 files to GAE. If you add more gems to the jar file, you can do it like this

  • unpacking it
  • add gems
  • and repack it

Enjoy!

posted by Png takiuchi on Thu 23 Apr 2009 at 23:32 with 2 comments
posted by Png genki on Thu 23 Apr 2009 at 21:12

Rails勉強会#40のセッションでも言いましたが、 エラーが発生した時等に表示されるbacktraceの表示は、 コンソールのように上から下に流れる画面上では 通常の逆順のほうが追いやすいと思います。

ということで、ちょっと試しにそのような挙動にしてみました。 以下のようなコードを実行するとテストできます。

   1  class Exception
   2    def set_backtrace(bt)
   3      puts bt.reverse.map{|i|
   4        i.sub(/^([\/\.\w]+):(\d+)/,
   5          "\e[33m\\1\e[m:\e[32m\\2\e[m")
   6      }.join("\n")
   7    end
   8  end
   9  
  10  def foo
  11    raise StandardError
  12  end
  13  
  14  def bar
  15    foo
  16  end
  17  
  18  bar

こんな感じに、エラーが発生した場所に近い順に、 下から上にたどっていく感じに表示されます。

ss

上から下のbacktraceに目が慣れてしまっているので、最初は追いかけにくさを感じますが、一旦慣れてしまえば、スタックが深くて相当スクロールアップしなければエラーの発生源までたどり着けないという場合には良さそうな気がします。

stackという概念のイメージのせいで、新しいものが上の方に来るようになっているのだと思いますが、実用上は新しいものが下にくるこのスタイルの方が便利ではないでしょうか。

posted by Png genki on Thu 23 Apr 2009 at 14:27 with 2 comments

情報系学生必須(?)のTeX環境をWindowsにインストールします。

これで、論文・レポートなど、ばっちりです!

ここでは、WindowsでのTeX環境のほかに、エディタのEmacsやtex-modeのYaTeXを導入します。

Vi派の人は今すぐこのページを閉じましょう。

TeXの導入

阿部 紀行さん作のTeXインストーラを使いましょう。

ここからダウンロードできます。

解凍するとインストーラが出てくるので、適当にインストールしましょう。

Emacsの導入

Windowsで使えるEmacsはいくつかありますが、ここではNTEmacsを使います。

ここから適当にダウンロードして、インストールします。

.emacsとか.emacs.dとかの場所

Windows Vistaの場合

Vistaの場合は、

   1  C:\Users\(ユーザ名)\AppData\Roaming

の下にあるものが読み込まれるようです。 なければ作りましょう。

Windows XPの場合

誰か教えてください!

YaTeX(野鳥)の導入

YaTeXとは、Yet AnotherなEmacsのtex-modeです。

手に入れる

Yet Another LaTeX mode for Emacs.からダウンロードしましょう。

ダウンロードしたら解凍し、.emacs.dあたりにyatexという名前で放り込んでおきましょう。

設定する

Yet Another tex-mode for Emacs - 野鳥起動のための設定を参考にします。

.emacsに以下のものをコピー&ペースト

   1  ;;
   2  ;; YaTeX
   3  ;;
   4  (setq auto-mode-alist
   5        (cons (cons "\\.tex$" 'yatex-mode) auto-mode-alist))
   6  (autoload 'yatex-mode "yatex" "Yet Another LaTeX mode" t)
   7  (setq load-path (cons (expand-file-name "~/.emacs.d/yatex/") load-path))

これで設定は終わりです。

使い方は、YaTeXのドキュメントを参考のこと・・・。

この環境が手に入ったら、ついでにOMakeを導入してみてはどうでしょうか?

posted by Png y_tsuda on Wed 22 Apr 2009 at 20:28

JRubyで相対パスを含む場合に、Jarファイルの中のファイルを読めるようにするためのモンキーパッチの最新版です。

   1  def cleanup_path(path)
   2    if path.to_s.match(/^file:/) && path.is_a?(String)
   3      jar_path, inner_path = path.split('!', 2)
   4      inner_path = File.expand_path(inner_path)
   5      path = [jar_path, inner_path].join('!')
   6    end
   7    path
   8  end
   9  
  10  alias :require_original :require
  11  def require(path)
  12    require_original cleanup_path(path)
  13  rescue Exception => e
  14    raise e unless path.to_s.match(/^file:/)
  15  end
  16  
  17  alias :load_original :load
  18  def load(path)
  19    load_original cleanup_path(path)
  20  rescue Exception => e
  21    raise e unless path.to_s.match(/^file:/)
  22  end
  23  
  24  class Dir
  25    class << self
  26      alias :aref_original :[]
  27      def [](*args)
  28        aref_original *(args.map{|path| cleanup_path(path)})
  29      end
  30    end
  31  end
  32  class File
  33    class << self
  34      alias :mtime_original :mtime
  35      def mtime(path)
  36        if path.match(/^file:/)
  37          jar_file, = path.split('!', 2)
  38          path = jar_file.sub(/^file:/, '')
  39        end
  40        mtime_original(path)
  41      end
  42    end
  43  end

posted by Png genki on Tue 21 Apr 2009 at 02:43

Merbでconfig/router.rbの中で、リソースのカスタムアクションを追加する方法のメモ。

   1  resource :users,
   2    :collection => {:active => :get},
   3    :member => {:password => :get}

:collectionで指定したアクションは、resource(:users, :active) などのように参照できて、"/users/active" のような感じになります。

:memberで指定したアクションは、resource(@user, :password) などのように参照できて、"/users/1/password" のようなスタイルになります。

posted by Png genki on Mon 20 Apr 2009 at 14:57

アプリをリリースするとき、日付はとても大事です。
(プレスリリースなどに関係するし)

Shopping Pinkをリリースしようとした際、iTunes ConnectにあるAvailability Dateについての詳しい情報があまりなくちょっと困りました。で、その時の経験でわかったのは、

その国のローカル時間でStoreに出される(ようだ)

Availability Dateを4/20にすると、日本のStoreには4/20、USのStoreにはUS時間の4/20ということです。日本だと時差がないので、全国同時ですが、USとかだとどうなるのかしら?ちょっとそこまではわかりませんでした。

posted by Png satoko on Mon 20 Apr 2009 at 10:09