Comment #85

# of names of a method

Added by Yuki Sonoda 279 days ago. Updated 271 days ago.

Status:Open Start:12/02/2009
Priority:Normal Due date:
Assigned to:- % Done:

0%

Category:-
Target version:-
Clause:

6.1

Page and Line:

21:33


Description

A method has one or more (when aliased) names associated with it.

According to Matz, a method in Ruby is a first class object and it can be referred via Object#method.
It means Method object is a method itself but not a proxy to method invocation. see http://yugui.jp/articles/741

So method can have no name as the following.
class C
  def m; end
end
c = C.new
m = c.method(:m)
class C
  remove_method :m
end

Now the method m has no name.

History

Updated by Shyouhei Urabe 279 days ago

It's not an objection, just to point a fact. This spec draft does not have Object#method. So it's technically possible for a standard-conforming Ruby processor to ignore Matz's intension.

Updated by Shugo Maeda 279 days ago

I have written the following comment as one of Ruby users/developers.

Yuki Sonoda wrote:

According to Matz, a method in Ruby is a first class object and it can be referred via Object#method. It means Method object is a method itself but not a proxy to method invocation. see http://yugui.jp/articles/741

Did he really say that, in the sense that a Method object is a method itself?

I don't think a Method object is a method itself because a Method object represents a method which is bound to the specific receiver, but the term "method" in the draft doesn't mean it.

Module#instance_method returns an object that represents a method which is not bound to the specific receiver. However, it returns an newly created instance for each invocation. So I think an UnboundMethod object is not also a method itself.

In the past, there are no method objects in Ruby, and I proposed BoundMethod and Object#bind in [ruby-dev:1002].
However, I don't think such a object is a method itself both now and then.

Updated by nobuyoshi nakada 279 days ago

But p m shows:
#<Method: C#m>

The name is kept in the Method object, and you can't create true anonymous Method object.

Updated by Yuki Sonoda 279 days ago

The name is kept in the Method object

It is a very implementation depended matter.

But I agree that, at least without Object#method nor Module#instance_method, any nameless method cannot appear.

Updated by Shugo Maeda 278 days ago

Yuki Sonoda wrote:

The name is kept in the Method object

It is a very implementation depended matter.

Is it?

How do you implement super in a Method object without a name?

I think the following code should print "Foo#foo" and "Bar#foo", but don't you think so?

class Foo
  def foo
    p "Foo#foo" 
  end
end

class Bar < Foo
  def foo
    super
    p "Bar#foo" 
  end
end

o = Bar.new
m = o.method(:foo)
m.call

Updated by Yuki Sonoda 277 days ago

I think the following code should print "Foo#foo" and "Bar#foo", but don't you think so?

I agree. I was wrong.

A method certainly has its original name at least, though the name may be no longer bound with the method.
class Foo
  def foo
    p "Foo#foo" 
  end
end

class Bar < Foo
  def foo
    super
    p "Bar#foo" 
  end
  alias_method :bar, :foo
  undef_method :foo
end

o = Bar.new
m = o.method(:bar)

class Foo
  def foo
    p "redefined Foo#foo" 
  end
end

m.call

This shows that a Method must hold its original name rather than the overridden method itself in the superclass, and that the name of super sometimes is not the name of the method. Of course, if remove_method'ing all bindings to the method, the method holds its original name but any binding does not bound to the method.

When a name N is bound to a method M, N is called the name of the binding, and M is called the value of the binding. A name to which a method is bound is called the method name.

Now I think the original name of a method should be distinguished from the method name in term as the above.

Updated by Shugo Maeda 271 days ago

Yuki Sonoda wrote:

When a name N is bound to a method M, N is called the name of the binding, and M is called the value of the binding. A name to which a method is bound is called the method name.

Now I think the original name of a method should be distinguished from the method name in term as the above.

The original name is described as the "defined name" in 96:3.
Should we add a reference to it in 6.1?

Also available in: Atom PDF