• 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
 
 

Rubyスクリプト中から

   1  require "hoge"

されている hoge のソースを見てみたい時には、

   1  vim `gem which hoge`.rb

すると見つけてきてくれます。

posted by Png genki on Sun 8 May 2011 at 17:22

CDH3に同梱されているgen-rbファイル群は若干古いらしく、上手く動かないのでif/hadoopfs.thrift を使って手動で生成します。

   1  % thrift --gen rb /path/to/if/hadoopfs.thrift

これで gen-rb/ ディレクトリの中にRuby用インターフェイスが生成されます。

posted by Png genki on Sun 8 May 2011 at 10:47 with 1 comment

monk-glue-0.0.1 に含まれている Monk::Glue::Reloaderには微妙な バグがあり、developmentモードでソースコードを更新したときに 自動リロードが上手くかからない事があるようです。

Rack::Reloaderの問題かと思っていたのですが、 調べてみると Monk::Glue::Reloader の処理に問題があることがわかりました。

reload!メソッドを以下のように修正したところ正常に動作することが確認できました。

   1    def reload!
   2      expanded_loaded_features = $LOADED_FEATURES.map do |path|
   3        File.expand_path(path)
   4      end
   5      hash = Hash[expanded_loaded_features.zip($LOADED_FEATURES)]
   6      deletes = []
   7      files.each do |file|
   8        if path = hash[File.expand_path(file)]
   9          $LOADED_FEATURES.delete(path)
  10        end
  11      end

$LOADED_FEATURESに絶対パスと相対パスが混在していることがあるようで、 その場合にうまく処理できていないようでした。

$LOADED_FEATURESは全て絶対パスで保持するようにしたほうが良い気がします。

posted by Png genki on Fri 22 Apr 2011 at 15:34

The main reason of which I was felt in love with Merb is it uses same context for controller and view. By sharing same context, view_helpers and instance variables become simple. You just use them in controller then you can use them in view by same manner. But as you know, Rails3 doesn't.

The reason Rails3 doesn't is probably for caching. If the view has many outer variables that affects its result, the cache key tends to be very complicated. In the manner of Rails3, we can use both action caching and partial caching easily. Because the all variables used in view are clear and small.

But in these days, I am doubting the use of those caching. Now we don't use browsers that has no capability of JavaScript. In addition, Google treats Ajax pages for indexing well. Haven't you ever seen the URI that has '#!'? That's for it. So now, the most of pages can be provided as static html with dynamic JSON. They are combined by JavaScript. There's few situations that the action caching and the partial caching go well.

In conclusion, I think the context should be shared between view and controller so that we can use view helpers simply.

posted by Png genki on Wed 12 Jan 2011 at 09:07

I added a new feature to irb_rocket. It's a simple utility for ease of daily irb use. Have you ever felt annoying for typing left cursor to enclose exprs by parens to be receiver like this?

   1  irb> 1 + 2 + 3  # Oh, I want to call "to_s"
   2  irb> (1 + 2 + 3 # back to the head
   3  irb> (1 + 2 + 3).to_s # return to the tail

I've experienced stuffs like this very often. Very annoying.

So I added the new feature named "irb_dollar" Its usage is simple, like this.

   1  irb> require "irb_dollar"
   2  irb> 1 + 2 + 3 $ to_s #=> "6"

Nice!

You can use "$" sign to make the left hand statement be receiver by installing irb_rocket version that is newer than 0.2.0

Enjoy :)

Links

posted by Png genki on Mon 3 Jan 2011 at 19:18

MacOS 10.6でmysql gemを使っていると発生するようだ。

   1  % sudo ARCHFLAGS="-arch x86_64" gem install mysql

こんな感じにgemをインストールすればokでした。

posted by Png genki on Wed 13 Oct 2010 at 20:13

しばらくGemをリリースしてなかったですが、gemcutterを使って 以下のようにすれば、rubygems.orgにgemをリリースできるようです。

   1  % gem push pkg/hoge-x.y.z.gem

rubygems.orgにアカウントを作っておき、gemcutterをgem installしておく必用があります。 非常に簡単になりましたね。

posted by Png genki on Wed 6 Oct 2010 at 02:02

Thorを使ってタスクを追加する場合、 通常以下のような感じで名前空間付きのタスクが追加されます。

   1  "クラス名":"タスク名"

グローバルな名前空間にタスクを追加したい場合は、

"help" という名前のタスクを追加すれば良いようです。

posted by Png genki on Sun 15 Aug 2010 at 05:53

中置記法と暗黙変換のコンビネーションでRubyの後置rescue的なものをScalaで実装してみました。

   1  class Rescueable[A](a: => A) { 
   2    def rescue[B](b: => B) = try{a}catch{case _ => b}
   3  }
   4  
   5  implicit def anyToRescueable[A](a: => A) = new Rescueable(a)
   6  
   7  println("12341234".toLong rescue 0)  // => 12341234
   8  println("1234x1234".toLong rescue 0) // => 0

call-by-nameがポイントです。

Update

bがcall-by-nameになっていなかったので修正しました。

posted by Png genki on Tue 11 May 2010 at 11:06
7th Fri

Ruby vs Scala

RubyistがScalaを簡単に把握できるように、 Google Spreadsheet で対応表をつくってみようかと思いました。

気が向いたら項目を追加していきます。

だれでも編集できる状態なので、加筆修正welcomeです :)

See Also

posted by Png genki on Fri 7 May 2010 at 21:12