Comment on this RCR (edit wiki page) | RCRchive home

RCR 138: Make def return something useful

submitted by anonymous on 2003-05-17 09:39:05


This is a legacy RCR. If this is your RCR, please resubmit it using the new format and process.


I'd really like def to return something useful like the Symbol of the newly created Method. -- This would make it possible to do something like this:

private def x
  do internal stuff
end

You might even want to return the created Method itself instead of its Symbol. This however would need public/protected/private changed a bit so they will take a Method as their parameter and use its Symbol.


Vote for this RCR

Strongly opposed [0]
Opposed [3]
Neutral [3]
In favor [8]
Strongly advocate [33]

Change the status of this RCR to:

accepted

rejected

withdrawn


Add comments here

Re: Make def return something useful (, 2003-05-17 15:27:34)

This however would need public/protected/private changed a bit so they will take a Method as their parameter and use its Symbol.

...but Methods don't have Symbols.

Re: Make def return something useful (, 2003-05-19 03:00:28)

why not ? In the Ruby User Guide you can see an example from matz, using the same symbol for a method, a class, and an object

Why not return an UnboundMethod *and* the Symbol? (, 2003-05-26 17:11:00)

Why don't we just let def return a little struct (or something similar) which contains both the UnboundMethod and the Symbol?

This would allow us to do even more things with it like (example from ruby-talk-posting by Dave Thomas)


    
  externally_typed(String) def get_name
    @first + " " + @last
  end
or the already mentioned
    

    
  private def x
    do internal stuff
  end

    

Of course private and friends would need to be slightly altered for this:


    
  alias :old_private :private
  def private(*args)
    if args and args[0] and args[0].is_a? MethodData
      old_private shift(args).symbol, *args
    else
      old_private *args
    end
  end

  

Well.. (root, 2003-05-29 17:33:06)

I'd argue that UnbondMethod should have an accessor that returns the name as a Symbol.

Hmmm... (ntalbott, 2003-06-17 21:43:45)

Well, I was going to post in agreement, but as I sit here thinking about it, I have to ask... does an UnboundMethod really have a name? Isn't it, by definition, unbound to any given name or thing? Or am I misunderstanding what an UnboundMethod really is?

Re: Hmmm... (johnplatte, 2003-06-18 10:26:20)

BoundMethod?

anonymous function, unbound/bound (, 2003-07-11 12:21:38)

[My name is Harald Gutsche from Germany]

just to throw some cents in...

I think, functions really should be objects (because everything should be)

so, this should be possible:

# anonymous function
f = def (x); puts x; end; f(77)

also this:

SomeClass.f = f; x = SomeClass.new; x.f(88)
also
x = SomeClass.new; x.f = f; x.f(88)

a bound method should carry it's (definition) name,

 def f; end # bound
differs from
 f = def; end # unboundf.name
should give the name

Functions are exactly like blocks but with different scope semantics, they don't have access to lexical variables around them, but blocks have.

So, one could have a function which turns a block into a function:

add = function begin |x,y|; x+y; end

which is equivalent to

add = def (x,y); x+y; end

so,

add = def |x,y|; x+y; end

should also be possible

After some thinking... (flgr, 2003-11-15 16:27:24)

I come to the conclusion that it should return a Method-object. And Method-Objects should have a to_sym method defined as something which produces an equivalent result as this one, but in a nicer way

class Method
  def to_sym
    self.to_s[/)#(.*)>$/, 1].intern
  end
end



Make def return something useful (dmiceman, 2003-11-28 00:12:24)

i`m also think what _all_ language constructions should return something. not only def, but also class and module.

def should return new Method object.

Back to RCRchive.


RCR Submission page and RCRchive powered by Ruby, Apache, RuWiki (modified), and RubLog