KagemushaでActiveRecordのfindなどのクラスメソッドをswap
しようとすると、

pre>>
singleton method bound for a different object (TypeError)
<<--

というエラーが発生するという問題がありました。

このままでは困るので、とりあえず動くようにしてみました。
Unboundにしなければ問題は起こらないと思ったので、
instance_methodの代わりにalias_methodを使っています。

kagemusha_core_ext.rb

ruby>>
require 'uuidtools'

class Kagemusha #:nodoc:
def swap #:nodoc:
original_class_methods = {}
original_instance_methods = {}

@class_methods.each { |name, proc|
  if proc
    begin
      # replace method
      #method = @meta.instance_method(name)
      method = random_name
      @meta.instance_eval { alias_method method, name }
      @meta.instance_eval { define_method(name, proc) }
      original_class_methods[name] = method
    rescue NameError
      # insert method
      @meta.instance_eval { define_method(name, proc) }
      original_class_methods[name] = false
    end
  else
    begin
      # remove method
      #method = @meta.instance_method(name)
      method = random_name
      @meta.instance_eval { alias_method method, name }
      @meta.instance_eval { undef_method(name) }
      original_class_methods[name] = method
    rescue NameError
      # nop
    end
  end
}

@instance_methods.each { |name, proc|
  if proc
    begin
      # replace method
      method = @klass.instance_method(name)
      @klass.instance_eval { define_method(name, proc) }
      original_instance_methods[name] = method
    rescue NameError
      # insert method
      @klass.instance_eval { define_method(name, proc) }
      original_instance_methods[name] = false
    end
  else
    begin
      # remove method
      method = @klass.instance_method(name)
      @klass.instance_eval { undef_method(name) }
      original_instance_methods[name] = method
    rescue NameError
      # nop
    end
  end
}

return yield

ensure
original_class_methods.each { |name, method|
if method
# replace method
#@meta.instance_eval { define_method(name, method) }
@meta.instance_eval { alias_method name, method }
@meta.instance_eval { undef_method method }
else
# remove method
@meta.instance_eval { undef_method(name) }
end
}
original_instance_methods.each { |name, method|
if method
# replace method
@klass.instance_eval { define_method(name, method) }
else
# remove method
@klass.instance_eval { undef_method(name) }
end
}
end

private
def random_name
[UUID.random_128].pack("m").tr("=\n", '')
end
end
<<--

posted by genki genki on Sun 6 Jul 2008 at 02:28 with 2 comments
Yuya Yuya 07/09 14:17
こんにちは。Kagemushaの作者のYuyaです。

不具合のご検討、ありがとうございます。
違う方法ではありますが、修正を入れてみました。
もしよろしければ、trunk版をお試し頂けると幸いです。

http://kagemusha.rubyforge.org/svn/trunk/kagemusha/lib/kagemusha/core.rb
瀧内元気 瀧内元気 07/09 16:02
おお、どうもありがとうございます。便利に使わせていただいています。
Specのカバレッジを上げるのに重宝しています。
Contents rssrss
光ファイバーを二次元振動させて走査するAR用ディスプレイ
因果の取り違え
Swift2's defer for CoffeeScript
mongodb-3.0からcreateIndexのdropDupsが無くなったらしい
mongodb-3.0以降のWiredTigerの設定を動的に変更する方法
一般楕円の高速生成アルゴリズムへの道標
farro mantecatoのレシピ
Droonga関連の記事のまとめ
RuntimeErrorの特定のメッセージに限定してrescueする方法
jQueryでscriptタグを実行せずにappendする
Tags
coffeescriptdefergroongajsmemonodenodejs
Comments rssrss
https://www.thegameroom.org/de/sportwetten/neu/ https://www.thegameroom.org/de/sportwetten/neu/: Развлечения для взрослых доступен через безопасные... 05/30 23:29
https://zwinkle.co/2026/05/24/emotional-availability-the-quality-that-determines-whether-love-can-actually-grow/ https://zwinkle.co/2026/05/24/emotional-availability-the-quality-that-determines-whether-love-can-actually-grow/: When some one searches for his required thing, the... 05/30 23:19
http://dailyplaza.co.kr/bbs/board.php?bo_table=1301&wr_id=297372 http://dailyplaza.co.kr/bbs/board.php?bo_table=1301&wr_id=297372: Experience Singapore’ѕ leading furniture store a... 05/30 22:47
https://meneercasino.com/slot-reviews/wolf-gold https://meneercasino.com/slot-reviews/wolf-gold: Смотрите видео для взрослых на безопасных и наде... 05/30 22:36
http://82.165.16.39/index.php?title=User:MadelineWysocki http://82.165.16.39/index.php?title=User:MadelineWysocki: пинап играть 05/30 22:28
http://series60.ru/go.php?url=https://www.518fans.com/ http://series60.ru/go.php?url=https://www.518fans.com/: Article writing is also a fun, if you be familiar ... 05/30 20:49
https://www.1337games.org/online-casinos/betaalmogelijkheden/ideal/ https://www.1337games.org/online-casinos/betaalmogelijkheden/ideal/: 优质xxx平台 提供高质量的成人娱乐内容。选择 有保障的平台 以获得安全且愉快的观看体验。 05/30 20:44
https://scapaflow.org https://scapaflow.org: Thank you for the good writeup. It in fact was a a... 05/30 19:28
Services from s21g
YOMU Web小説リーダー
小説投稿サイトの公式ページを開き、WebView表示と読み上げ向け表示を切り替えて、移動中や作業中にもWeb小説の続きを聴きやすくするiPhoneアプリです。
補助探
公開されている補助金・助成金情報を集約し、条件に合う制度を探しやすくするサービスです。
jotter.me
個人開発者のためのホスティング一体型ノートサービス
ハンドミラー
iPhone向けの手鏡アプリ
ツイプロ(twpro)
Twitterプロフィールの高速検索エンジン