On Sat, Nov 20, 2004 at 03:58:10AM +0900, Daniel Berger wrote:
> > | I'm trying to write a class where I want to delegate some instance
> > | methods as FileTest class methods.  For example:
> > |
> > | require "forwardable"
> > | class Foo < String
> > |    extend Forwardable
> > |    def_delegator(:FileTest,:directory?)
> > |    def initialize(path)
> > |       @path = path
> > |    end
> > | end
> > 
> > Not sure I understand, but is this what you are trying to do?
> > 
> >   require "forwardable"
> >   class Foo < String
> >      extend Forwardable
> >      def directory?
> >        FileTest.directory?(@path)
> >      end
> >      def initialize(path)
> >        @path = path
> >      end
> >   end
> I knew I could do that, but I was trying to find a shortcut rather
> than define each method from FileTest manually.  However, the syntax
> I'm proposing isn't going to save much in terms of keystrokes, so
> maybe it isn't worth it after all.

It can be done cleanly under 1.9 with Object#instance_eval and
Module#define_method; the syntax would look like
  def_delegator(:FileTest, :directory?){ @path }

Under 1.8, you can easily have something like
  def_delegator(:FileTest, :directory?, :directory?, "@path")
(2nd :directory? for compatibility with forwardable.rb) using
Module#module_eval.

-- 
Hassle-free packages for Ruby?
RPA is available from http://www.rubyarchive.org/