Method.new - why not?

D

Daniel Berger

Hi,

Just a random thought that I think was inspired by several people on
the list asking about getting the name of the method from within the
method - why don't we have Method.new? In other words, what if this:

def foo(x, y=1)
end

Was the equivalent of this?

x = Method.new:)name => 'foo', :parameters => ['x', 'y'], :defaults =>
{'y' => 1})
Object.include(x) # or bind(x) or something

Similar to an UnboundMethod I suppose, but with more options. Wouldn't
it be easier to get method metadata with this approach? And give
programmers the ability to associate any methods with any class or
module?

class Bar
def foo(x, y=1)
p method:)foo).metadata # => @name => 'foo', @parameters =>
['x','y'], :defaults => {'y' => 1})
end
end

Or is this totally stupid?

If this has been discussed before just point me to the thread.

Regards,

Dan
 
R

Robert Dober

Hi,

Just a random thought that I think was inspired by several people on
the list asking about getting the name of the method from within the
method - why don't we have Method.new? In other words, what if this:

def foo(x, y=1)
end

Was the equivalent of this?

x = Method.new:)name => 'foo', :parameters => ['x', 'y'], :defaults =>
{'y' => 1})
Object.include(x) # or bind(x) or something

Similar to an UnboundMethod I suppose, but with more options. Wouldn't
it be easier to get method metadata with this approach? And give
programmers the ability to associate any methods with any class or
module?

class Bar
def foo(x, y=1)
p method:)foo).metadata # => @name => 'foo', @parameters =>
['x','y'], :defaults => {'y' => 1})
end
end

Or is this totally stupid?

If this has been discussed before just point me to the thread.

Regards,

Dan
Not stupid at all. I would even suggest that we do not bind methods to
object's class anymore.
Class is just a convenient? place for method err forget that word,
behavior lookup after all.

a = def a *args, &blk; whatever end
shall yield exactly the same as
a = lambda{ |*args,&blk| whatever }

But than a could implement the whole API above.
In other words, either Proc or Method has to die, one is redundant!

object.define_method a
object.extend a

Now the language implementors will probably just kill me, but that is
a risk I have to take ;).

R.
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top