From: Avdi Grimm Date: 2005-04-14T22:39:37+09:00 Subject: Re: Mocking out a class object "David A. Black" writes: > You're actually dealing with two classes: MyMockClass, and > MyMockClass's singleton class. So you would probably want to do > something like this: > > # This is to make it look nicer (as per the > # pending RCR :-) > module Kernel > def singleton_class > class << self; self; end > end > end > > my_instance = "" > > MyMockClass = Class.new { > self::FOO = "some value" > singleton_class.class_eval { > define_method(:new) {|*| my_instance} > } > } > > p MyMockClass::FOO # "some value" > p MyMockClass.new # "" Thanks, that looks exactly like what I was looking for. It reveals that I don't understand Ruby metaprogramming quite as well as I thought, though. Would you mind explaining what singleton_instance is doing, and how it differs from just using 'self' in the class definition? Thanks, Avdi