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

  • Thread starter Wolfgang Nádasi-Donner
  • Start date
W

Wolfgang Nádasi-Donner

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.

___________

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"]
___________


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

Wolfgang Nádasi-Donner (WoNáDo)
 
W

Wolfgang Nádasi-Donner

I don't know where to put this message into the very large discussion tree, so I
will put it here.

There is an argument against naming a method "receiver", which returns the
object which was bound to a method object.

class Otto
def hi
puts "'Hi!' from an 'Otto' instance (#{self.inspect})"
end
end

class Hugo < Otto
def hi
puts "'Hi!' from an 'Hugo' instance (#{self.inspect})"
end
end

o = Otto.new
h = Hugo.new
bo = o.method:)hi)
bh = bo.unbind.bind(h)

h.hi # => 'Hi!' from an 'Hugo' instance (#<Hugo:0x2aea4dc>)
bh.call # => 'Hi!' from an 'Otto' instance (#<Hugo:0x2aea4dc>)

In this example, the "Method" object "bh" contains (I temporary use this wording
for this example) the object "h", and the method "Otto#hi", which can be seen
in the output of "bh.call" - but - if ones sends the message "hi" to the object
"h" the method "Hugo#hi" will be used (see output for "h.hi").

This means, that for the object "h" contained in "Method" object "bh" a message
"hi" will not end up in calling "Otto#hi", because it finds "Hugo#hi".

It is misleading to call "h" the "receiver" of a message, that leads to the
invocation of "Otto#hi", because this cannot be done by any message in this example.

May be a different name is better to avoid confusion.

Wolfgang Nádasi-Donner
 
W

Wolfgang Nádasi-Donner

Sorry, I didn't recognize, that this was already named. More than 90 posts in
this discussion are very much...

Wolfgang Nádasi-Donner
 
W

Wolfgang Nádasi-Donner

Hi!

I don't know how you handle "Change Proposals" for Ruby. Implement them silently
or write somewhere an announcement, so I try to summarize what happens, as I
understood it.

Please correct me if I'm wrong, because I will report about the results in the
german Ruby forum.

I include my proposal for class "UnboundMethod" here, because I think it is
strongly related to this one.

----------------------------

As I understood, the will be some new methods available now or in the near
future in the repository (or the nightly snapshot). I'm not sure about the
chosen names, so please correct them.

==============
Class "Method"
==============

Public instance Methods:

receiver
--------
"receiver" returns the bound object of the method object. E.g. for "mo =
o.method:)m)" the expression "mo.receiver" will return "o".

name
----
"name" returns the name of the method object. E.g. for "mo = o.method:)m)" the
expression "mo.name" will return ":m" (symbol) or "'m'" (string).

owner
-----
"owner" returns the class which holds the method. E.g. for "mo = o.method:)m)"
the expression "mo.owner" will return "c", if the method ":m", that is bound to
"mo", is defined in this class. If the class is an anonymous class, it will be
handled correctly.

=====================
Class "UnboundMethod"
=====================

Public instance Methods:

name
----
"name" returns the name of the method object. E.g. for "mo = o.method:)m)" the
expression "mo.name" will return ":m" (symbol) or "'m'" (string).

owner
-----
"owner" returns the class which holds the method. E.g. for "mo = o.method:)m)"
the expression "mo.owner" will return "c", if the method ":m", that is bound to
"mo", is defined in this class. If the class is an anonymous class, it will be
handled correctly.

-----------------------

Is it a correct summary. There are so many messages in the discussion, that I
ended up in comfusion about the chosen names, sorry.

Wolfgang Nádasi-Donner
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top