Delegating a method, with arguments

D

Daniel Berger

Hi all,

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

This doesn't work, however, since FileTest.directory? requires an
argument. What I would like is something like the following fictional
syntax:

def_delegator:)FileTest,:directory?(@path))

I don't want to create an instance of File and delegate to that
instance because I don't know if @path will actually be valid or not
(among other reasons).

So, would something like this be possible?

Regards,

Dan
 
T

trans. (T. Onoma)

On Friday 19 November 2004 12:48 am, Daniel Berger wrote:
| Hi all,
|
| 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


?

T.
 
Z

Zach Dennis

Daniel said:
Hi all,

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


If you said that def_delegator always in the form of: def_delegator(
class, method, arg1, arg2, arg3, etc... )

Then I think you could do: def_delegator( :FileTest, :directory? , :arg1 )

Or you could do something like: def_delegator( :FileTest,
:directory?=>[arg1] )


Zach
 
R

Robert Klemme

Daniel Berger said:
Hi all,

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

This doesn't work, however, since FileTest.directory? requires an
argument. What I would like is something like the following fictional
syntax:

def_delegator:)FileTest,:directory?(@path))

I don't want to create an instance of File and delegate to that
instance because I don't know if @path will actually be valid or not
(among other reasons).

So, would something like this be possible?

This is easy but doesn't keep arity:

class Module
def def_delegator(instance_var, *syms)
syms.each do |m|
class_eval "def #{m}(*a,&b) #{instance_var}.#{m}(*a,&b) end"
end
end
end

class Foo
attr_accessor :bar
def_delegator("@bar", :foo)
end

class Bar
def foo() "yes" end
end
=> "yes"

With more effort and the additional info of the target class you can
determine the target method's arity and use that. However, the only
benefit is that clients who ask your delegator method for it's arity see
the real method's arity.

Kind regards

robert
 
D

Daniel Berger

trans. (T. Onoma) said:
On Friday 19 November 2004 12:48 am, Daniel Berger wrote:
| Hi all,
|
| 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


?

T.

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.

Regards,

Dan
 
M

Mauricio Fernández

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top