# Method.new - why not?

**URL:** <https://www.ruby-forum.com/t/method-new-why-not/159399>\
**Category:** Ruby\
**Created:** [February 4, 2009, 7:23am UTC](https://www.ruby-forum.com/t/method-new-why-not/159399 "2009-02-04T07:23:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [February 4, 2009, 7:23am UTC](https://www.ruby-forum.com/t/method-new-why-not/159399/1 "2009-02-04T07:23:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [February 4, 2009, 8:18am UTC](https://www.ruby-forum.com/t/method-new-why-not/159399/2 "2009-02-04T08:18:32Z")

</div>

Isn’t that how it already works?? :). Sorta. :). Try this:  
File.method(:foreach)

Blog: [http://random8.zenunit.com/](http://random8.zenunit.com/)  
Learn rails: [http://sensei.zenunit.com/](http://sensei.zenunit.com/)

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [February 4, 2009, 10:20am UTC](https://www.ruby-forum.com/t/method-new-why-not/159399/3 "2009-02-04T10:20:02Z")

</div>

On Wed, Feb 4, 2009 at 7:20 AM, Daniel B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> def foo(x, y=1)
> 
> 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.

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [February 5, 2009, 2:38am UTC](https://www.ruby-forum.com/t/method-new-why-not/159399/4 "2009-02-05T02:38:17Z")

</div>

There’s nothing to stop him writing a new class, say Message, that  
exhibits this behaviour tho hey

Blog: [http://random8.zenunit.com/](http://random8.zenunit.com/)  
Learn rails: [http://sensei.zenunit.com/](http://sensei.zenunit.com/)

On 05/02/2009, at 2:29 AM, Yukihiro M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [February 4, 2009, 4:29pm UTC](https://www.ruby-forum.com/t/method-new-why-not/159399/5 "2009-02-04T16:29:50Z")

</div>

Hi,

In message “Re: Method.new - why not?”  
on Wed, 4 Feb 2009 15:20:21 +0900, Daniel B.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

|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

|Or is this totally stupid?

I don’t think the idea itself is stupid at all, but I am afraid that  
it doesn’t go quite well with Ruby’s Lisp-2 semantics. That idea  
would shine better in Lisp-1-ish languages like Scheme or Python.

```
          matz.

```
