Minor Change Proposal for Classes 'Object' and 'Method'

Minor Change Proposal for Classes ‘Object’ and ‘Method’


I would like to make a small change suggestion on the class ‘Method’ by
which
the method ‘Object#method’ is also affected.

Background:


When creating a ‘Method’ object, it is not possible to receive the
object
identification of the object which uses the ‘Object#method’ method (see
‘Example
for Workaround’ for details) by using some Method of class ‘Method’.

It is useful for some applications to analyse later on which to which
objects a
method is bound in an object of class ‘Method’. In addition, it is
simply
missing from my viewpoint, because it is an essential information
(attribute) of
an object of class ‘Method’.

A minor wish is an additional method for class ‘Method’, which returns
the
contents of ‘Method#to_s’ as an Array containing two elements, the
class-name
and the method-name without the textual border of ‘Method#to_s’. It is
easier
and less expensive to return this existing information by a method of
class
‘Method’, than to use a regular expression later on to extract the
information.

The method names used by here are only suggestions, since I needed names
for the
example. Regarding definite names I have no emotions.


Example for Workaround >>>>>
class Object
alias :_org_method :method
def method(name)
method_object = self._org_method(name)
method_object.instance_id = self.object_id
method_object
end
end

class Method
attr_accessor :instance_id
def method_name
md = self.to_s.match(/Method:\s*([^#]+)#([^>]+)>/)
return md[1], md[2]
end
end

Now an example

class Hugo
def hi
puts “An instance of Hugo says ‘Hi!’”
end
end

my_hugo = Hugo.new
puts my_hugo.object_id # => 22497820 (for example)
myhi = my_hugo.method(:hi)
myhi[] # => An instance of Hugo says ‘Hi!’
puts myhi.instance_id # => 22497820 (for example)
p myhi.method_name # => [“Hugo”, “hi”]

End of Example     >>>>>

Please inform me, if this is not the right place for a change proposal.

Wolfgang Nádasi-Donner (WoNáDo)

Hi,

In message “Re: Minor Change Proposal for Classes ‘Object’ and ‘Method’”
on Sat, 20 Jan 2007 01:15:05 +0900, Wolfgang Nádasi-Donner
[email protected] writes:

|Minor Change Proposal for Classes ‘Object’ and ‘Method’

|my_hugo = Hugo.new
|puts my_hugo.object_id # => 22497820 (for example)
|myhi = my_hugo.method(:hi)
|myhi[] # => An instance of Hugo says ‘Hi!’
|puts myhi.instance_id # => 22497820 (for example)
|p myhi.method_name # => [“Hugo”, “hi”]

Why object_id? Isn’t it more useful to return the receiver itself,
e.g.

myhi.receiver # => #Hugo:0xb7d5ff40

?

And why should method_name return combination of class name and method
name? I think the method_name should return the method name only
(“hi” for this case). It may be useful to implement separate method
to get a class that holds the method.

          matz.

Yukihiro M. schrieb:

to get a class that holds the method.
I agree, because I’m only interested in the information at all. If you
find a
better format or better way to present it, its fine.
One question is open for ‘myhi.receiver # => #Hugo:0xb7d5ff40’.

First, the format requires a pattern matching on the result, if the
object-id
ist needed only. May be two seperate methods are better, as you
suggested for
‘method_name’.

In additon, a format for the seperate method, that returns information
about the
class that holds the method, may be complicated for singleton methods.
An
example is ‘#<Method: #Hugo:0x2ae6e54.private_hi>’.

Wolfgang Nádasi-Donner (WoNáDo)

Wolfgang Nádasi-Donner:

One question is open for ‘myhi.receiver # => #Hugo:0xb7d5ff40’.

First, the format requires a pattern matching on the result, if the object-id
ist needed only.

May you elaborate? Where do you need pattern matching here?

Kalman

Yukihiro M.:

And why should method_name return combination of class name and method
name? I think the method_name should return the method name only
(“hi” for this case).

I agree here. Furthermore I think that #name would be a better name, in
analogy to Module#name.

Kalman

Hi,

In message “Re: Minor Change Proposal for Classes ‘Object’ and ‘Method’”
on Sat, 20 Jan 2007 03:05:10 +0900, Kalman N. [email protected]
writes:

|Wolfgang Nádasi-Donner:
|> One question is open for ‘myhi.receiver # => #Hugo:0xb7d5ff40’.
|>
|> First, the format requires a pattern matching on the result, if the object-id
|> ist needed only.
|
|May you elaborate? Where do you need pattern matching here?

I think he supposed receiver returns above string, but it doesn’t. It
returns the object reference. If you need object-id, you can get it
from

myhi.receiver.object_id

          matz.

Kalman N. schrieb:

Wolfgang Nádasi-Donner:

One question is open for ‘myhi.receiver # => #Hugo:0xb7d5ff40’.
May you elaborate? Where do you need pattern matching here?

myhi.receiver.match(/:([^>]+)>/)[1] # => “0xb7d5ff40”

and I need ‘eval’ to compare the textual presentation of the object-id
with the
object-id I receive from “Object#object_id” (or I have to convert this
result to
an textual hex-presentation)

Yukihiro M. schrieb:

I think he supposed receiver returns above string, but it doesn’t. It
returns the object reference. If you need object-id, you can get it
from

myhi.receiver.object_id

I see - I thought it is a string, sorry.

Wolfgang Nádasi-Donner

Hi,

In message “Re: Minor Change Proposal for Classes ‘Object’ and ‘Method’”
on Sat, 20 Jan 2007 01:52:36 +0900, Yukihiro M.
[email protected] writes:

|Why object_id? Isn’t it more useful to return the receiver itself,
|e.g.
|
| myhi.receiver # => #Hugo:0xb7d5ff40
|
|?

Wolfgang’s proposal can be divided into three methods.

#receiver that returns the bound object of the method object.
#name that returns the name of the method object.
and a method that returns the class which holds the method.

Does anybody have good name candidate for the last one? I have
already implemented the first two methods in the local copy of the
repository.

          matz.

On Jan 19, 2007, at 12:33 PM, Yukihiro M. wrote:

| myhi.receiver # => #Hugo:0xb7d5ff40
|
|?

Wolfgang’s proposal can be divided into three methods.

#receiver that returns the bound object of the method object.
#name that returns the name of the method object.
and a method that returns the class which holds the method.

Does anybody have good name candidate for the last one?

I’m kind of partial to #owner myself.

On Sat, 20 Jan 2007, Yukihiro M. wrote:

|?

Wolfgang’s proposal can be divided into three methods.

#receiver that returns the bound object of the method object.
#name that returns the name of the method object.
and a method that returns the class which holds the method.

Does anybody have good name candidate for the last one? I have
already implemented the first two methods in the local copy of the
repository.

object.where? “method_name” # returns nil if not defined

object.definer “method_name” # same as above

another idea:

m = object.def “method_name” # an uber-bound method object

p m.name #=> “method_name”
m.call *a, &b #=> same as object.method_name *a, &b

kind regards.

-a

Yukihiro M. wrote:

|?

Wolfgang’s proposal can be divided into three methods.

#receiver that returns the bound object of the method object.
#name that returns the name of the method object.
and a method that returns the class which holds the method.

Does anybody have good name candidate for the last one? I have
already implemented the first two methods in the local copy of the
repository.

Method#parent

Dan

Yukihiro M. wrote:

|?

Wolfgang’s proposal can be divided into three methods.

#receiver that returns the bound object of the method object.
#name that returns the name of the method object.
and a method that returns the class which holds the method.

Does anybody have good name candidate for the last one? I have
already implemented the first two methods in the local copy of the
repository.

What about origin ? Or even of ?

method.of => MyClass

Cheers,

Vince

On Jan 19, 2007, at 1:33 PM, Yukihiro M. wrote:

Wolfgang’s proposal can be divided into three methods.

#receiver that returns the bound object of the method object.
#name that returns the name of the method object.
and a method that returns the class which holds the method.

Does anybody have good name candidate for the last one? I have
already implemented the first two methods in the local copy of the
repository.

Method#receiver
Method#name
Method#origin # module/class with definition of method

Kernel#origin(name) # module/class where named method is found
# via standard method lookup process on the
# receiver.
# nil if no matching method found

Kernel#origin!(name) # same as origin but reports location of
# any matching method_missing implementation
# nil if no method_missing applies

Not sure about origin vs. origin! names but it seems reasonable
to be interested in whether method_missing is triggered or not.

I’ve often thought it might be nice to have a ‘magic’ reference
similar to ‘self’ that referenced a method object matching the
current receiver and method:

def factorial(n)
return 1 if n.zero?
myself[n-1] * n
end

So in that example, ‘myself’ would be an instance of Method bound
to the top-level object and the method Object#factorial.

Combined with the other proposed methods you get:

myself.name # => ‘factorial’
myself.receiver # => main
myself.origin # => Object

Not sure about the ‘myself’ name, but I like the idea.

Gary W.

[email protected] wrote:

Method#receiver
Method#name
Method#origin # module/class with definition of method

Kernel#origin(name) # module/class where named method is found
# via standard method lookup process on the
# receiver.
# nil if no matching method found

I vote for the last one too !

Vince

Vincent F. schrieb:

I vote for the last one too !

Vince

I don’t think it works well with “Kernel#origin(name)” because the
lookup result
may change when changing the class definitions. The method, which
returns the
class name, should be directly related to the existing “Method” object.
Example:

Example >>>>>
class Otto
def hi
puts “‘Hi!’ from an Otto instance”
end
end

class Hugo < Otto
end

o1 = Hugo.new
mymeth1 = o1.method(:hi)

class Hugo < Otto
def hi
puts “‘Hi!’ from a Hugo instance”
end
end

mymeth2 = o1.method(:hi)

mymeth1[] # => ‘Hi!’ from an Otto instance
mymeth2[] # => ‘Hi!’ from a Hugo instance

p mymeth1 # => #<Method: Hugo(Otto)#hi>
p mymeth2 # => #<Method: Hugo#hi>

End of Example >>>>>

Wolfgang Nádasi-Donner

On Jan 20, 2007, at 10:20 AM, Wolfgang Nádasi-Donner wrote:

I vote for the last one too !
Vince

An additional method “Kernel#origin(name)” will be helpful, if the
method is defined as a singleton method for an object, it is
difficult to get the object_id of the anonymous class, where the
method ist defined (I hope this sentense is somehow understndable).

The object_id is necessary in comparisons with the result of the
call to the method of class “Method”, that returns the associated
class.

I was assuming that Method#origin and Kernel#origin would both
return a reference to the class/module. In fact, Kernel#origin
is really just short for method(name).origin and of course
you can just call object_id on the module/class reference as needed.

It appears that you are viewing the (proposed) return value of
Method#origin and Kernel#origin as a string and not as a reference
to the module/class itself as intended by myself and the other
posters.

Gary W.

[email protected] schrieb:

        # via standard method lookup process on the

The object_id is necessary in comparisons with the result of the call
posters.
Sorry, I am a little bit confused in the moment…

What I like to say is, that in case of

class Otto
def hi
puts “Hi”
end
end
o = Otto.new
my1 = o.method(:hi) # Class Otto, Method hi
class << o
def hi
puts “Hihi”
end
end
my2 = o.method(:hi) # Class -anonymous-, Method hi

“Method#origin” returns the object_id of the class where the method
belongs too.
Thats clear for me. The problem I see is, that for the second
Method-object
“my2” “Method#origin” returns an object_id on an anonymous class.

If later on I want to find out if my Method objects reference a method,
that is
defined in an anonymous class, I will have problems to get the object_id
of this
anonymous class without an additional “Kernel#origin(name)” method, that
returns
the object_id of the anonymous class in case of (e.g) “o.origin(:hi)”.
The
result should not be the same before and after “class << o”.

Wolfgang Nádasi-Donner

On Jan 20, 2007, at 11:45 AM, Wolfgang Nádasi-Donner wrote:

“Method#origin” returns the object_id of the class where the method
belongs too. Thats clear for me.

No. the return value isn’t the object_id (a Fixnum). It is a
reference to the class/module itself. Same idea with Kernel#origin

This is the same pattern as with Kernel#class:

a = Hash.new
a.object_id # a Fixnum
a.class # reference to Hash, i.e. the class itself

b = a.class.new # a newly created instance of Hash

m = Hash.method(‘invert’) # an instance of Method
m.origin # a reference to Hash

h = m.origin.new # another instance of Hash is created

Gary W.

Wolfgang Nádasi-Donner wrote:

An additional method “Kernel#origin(name)” will be helpful, if the method is
defined as a singleton method for an object, it is difficult to get the
object_id of the anonymous class, where the method ist defined (I hope this
sentense is somehow understndable).

Could you explain what you mean by this? I would assume that this would
work:

class Foo; def bar1; end; end
f = Foo.new
def f.bar2; end

o1 = f.method( :bar1 ).owner
o2 = f.eigenclass # class << f; self; end
o3 = f.method( :bar2 ).owner
p o1 == Foo
#=> true
p o2 == o3
#=> true

The owner of an instance method should be the class on which it is
defined, right? Thus, the owner of a ‘singleton method’
(eigenmethod(?)) would be the (anonymous/singleton/eigen) class. That
would make it easy to get the object_id of that class for the such a
method.