RubiniusでBinding.of_caller的な

かつてActiveSupportにあったけど、Ruby1.8.5の登場で惜しまれつつ亡くなったBinding.of_callerが、Rubiniusだと簡単に実現できるとか。

# http://gihyo.jp/news/report/01/rubykaigi2008/0001
class Binding
  def self.of_caller
    Binding.setup MethodContext.current.sender.sender
  end
end


これは早速やってみなければ

$ rbx binding_of_caller.rb 
An exception occurred running binding_of_caller.rb
    Missing or uninitialized constant: Binding::MethodContext (NameError)

Backtrace:
       Module(Class)#const_missing at kernel/common/module.rb:529
                 Binding.of_caller at binding_of_caller.rb:3
                   main.__script__ at binding_of_caller.rb:7
  Rubinius::CodeLoader#load_script at kernel/delta/codeloader.rb:67
  Rubinius::CodeLoader.load_script at kernel/delta/codeloader.rb:91
           Rubinius::Loader#script at kernel/loader.rb:521
             Rubinius::Loader#main at kernel/loader.rb:619
             Rubinius::Loader.main at kernel/loader.rb:658
                 Object#__script__ at kernel/loader.rb:669


と思ってやってみたらなぜかうまく行かなくて


とかボヤいてたら


ほほー。ありがとう、知らん人。
・・・ってこの人がRubiniusの作者なのか!ありがとう、偉い人!!

class Binding
  def self.of_caller
     Binding.setup(
       Rubinius::VariableScope.of_sender,
       Rubinius::CompiledMethod.of_sender,
       Rubinius::StaticScope.of_sender
     )
  end
end


ということで、今はこれが正しいやり方みたい。

Rubiniusはものすごく面白そうだけどドキュメントが全然ないのがちょっと辛いなー。