Method.new - why not?

Discussion in 'Ruby' started by Daniel Berger, Feb 4, 2009.

  1. 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
     
    Daniel Berger, Feb 4, 2009
    #1
    1. Advertising

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

    Blog: http://random8.zenunit.com/
    Learn rails: http://sensei.zenunit.com/

    On 04/02/2009, at 5:20 PM, Daniel Berger <> wrote:

    > 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
    >
     
    Julian Leviston, Feb 4, 2009
    #2
    1. Advertising

  3. Daniel Berger

    Robert Dober Guest

    On Wed, Feb 4, 2009 at 7:20 AM, Daniel Berger <> wrote:
    > 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.


    --
    It is change, continuing change, inevitable change, that is the
    dominant factor in society today. No sensible decision can be made any
    longer without taking into account not only the world as it is, but
    the world as it will be ... ~ Isaac Asimov
     
    Robert Dober, Feb 4, 2009
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    982
    Mark Rae
    Dec 21, 2006
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,231
    Smokey Grindel
    Dec 2, 2006
  3. Skybuck Flying
    Replies:
    16
    Views:
    701
    tragomaskhalos
    Aug 25, 2007
  4. Elf M. Sternberg
    Replies:
    15
    Views:
    283
    Matthias Reitinger
    Jul 29, 2009
  5. Replies:
    2
    Views:
    509
    Thomas 'PointedEars' Lahn
    Mar 11, 2008
Loading...

Share This Page