On Mon, 22 Mar 2004 20:26:18 +0100, Simon Strandgaard wrote: > On Tue, 23 Mar 2004 02:15:18 +0900, Dave Thomas wrote: >> I'm being dense today, but can anyone think of a real-world application >> of SingleForwardable? I keep trying to come up with examples, but I >> haven't yet discovered any that aren't better written as methods inside >> the class itself. >> > > The only usage I can think of is unittesting. Imagine that the Values > class is some class that we didn't wrote and that we somehow needs to > populate it with data. > > > server> ruby a.rb > #<Values:0x810a5e4 @values=[1, 2]> > server> expand -t2 a.rb > class Values > def initialize > @values = [] > end > attr_reader :values > end > > # imagine that this is a testcase, located in a > # different file. > require 'forwardable' > v = Values.new > v.extend SingleForwardable > v.def_delegator("@values", :<<, "append") > v.append(1) > v.append(2) > p v > server> > > > I don't know if this can be useful.. some kind of mockobject is probably > better to use in this case. Here is the opposite example: Trying to reopen a class inside a method isn't possible. server> ruby a.rb a.rb:10: class definition in method body server> expand -t2 a.rb class Values def initialize @values = [] end attr_reader :values end class Test def test_values class Values def append(element) @values << element end end v = Values.new v.append(1) v.append(2) p v end end Test.new.test_values server> -- Simon Strandgaard