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

Discussion in 'Ruby' started by Wolfgang Nádasi-Donner, Jan 19, 2007.

  1. 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)
    Wolfgang Nádasi-Donner, Jan 19, 2007
    #1
    1. Advertising

  2. 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.

    >>>>> Example >>>>>


    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>)
    bo.call # => 'Hi!' from an 'Otto' instance (#<Otto:0x2aea478>)

    >>>>> EoE >>>>>


    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
    Wolfgang Nádasi-Donner, Jan 26, 2007
    #2
    1. Advertising

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

    Wolfgang Nádasi-Donner
    Wolfgang Nádasi-Donner, Jan 26, 2007
    #3
  4. 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
    Wolfgang Nádasi-Donner, Jan 30, 2007
    #4
    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. kbutterly
    Replies:
    1
    Views:
    352
    Jon Paal
    Feb 11, 2007
  2. Elf M. Sternberg
    Replies:
    15
    Views:
    259
    Matthias Reitinger
    Jul 29, 2009
  3. Wolfgang Nádasi-Donner

    Minor Change Proposal for Classe 'UnboundMethod'

    Wolfgang Nádasi-Donner, Jan 19, 2007, in forum: Ruby
    Replies:
    0
    Views:
    119
    Wolfgang Nádasi-Donner
    Jan 19, 2007
  4. Wolfgang Nádasi-Donner

    Minor Change Proposal for Class 'Numeric'

    Wolfgang Nádasi-Donner, Jan 22, 2007, in forum: Ruby
    Replies:
    7
    Views:
    106
    Wolfgang Nádasi-Donner
    Jan 23, 2007
  5. Wolfgang Nádasi-Donner
    Replies:
    1
    Views:
    105
    Wolfgang Nádasi-Donner
    May 15, 2007
Loading...

Share This Page