Confusion About UnboundMethod

J

James Herdman

I'm reading the last little fragment of the Pick Ax book during my exam
study breaks right now, and I'm contemplating this UnboundMethod
hotness.

Pick Ax says, "As with aliases, unbounch methods are references to the
definition of the method at the time they are created" with the example

[CODE BEGIN]
unbound_length = String.instance_method:)length)
class String
def length
99
end
end
str = "cat"
str.length -> 99
bound_length = unbound_length.bind(str)
bound_length.call -> 3
[CODE END]

The first part of my question is if I can read "... are references to
the definition of the definition of the method at the time they are
created" as being equivalent to "copy of"? (My assumption is yes,
given that my unbound copy isn't altered by redefinition #length.)

The second part of my question what about the contents of
unbound_length. Does it actually contain code, or is it just a memory
location? (I.E. is there a way I can have unbound_length spit out the
code for #length?)

Thank you for your time,

James H.
 
R

Robert Klemme

James said:
I'm reading the last little fragment of the Pick Ax book during my exam
study breaks right now, and I'm contemplating this UnboundMethod hotness.

Pick Ax says, "As with aliases, unbounch methods are references to the
definition of the method at the time they are created" with the example

[CODE BEGIN]
unbound_length = String.instance_method:)length)
class String
def length
99
end
end
str = "cat"
str.length -> 99
bound_length = unbound_length.bind(str)
bound_length.call -> 3
[CODE END]

The first part of my question is if I can read "... are references to
the definition of the definition of the method at the time they are
created" as being equivalent to "copy of"? (My assumption is yes, given
that my unbound copy isn't altered by redefinition #length.)

IMHO not. In the example there is an initial version of the method
"length". A reference to that is kept alive with "unbound_method".
Then there is a second version of the method created by the class body
that replaces the original def (but that is still accessible via
"unbound_length"). If you execute instance_method twice in the
beginning you get two UnboundMethod objects but both point to the same
piece of code.
The second part of my question what about the contents of
unbound_length. Does it actually contain code, or is it just a memory
location? (I.E. is there a way I can have unbound_length spit out the
code for #length?)

No. There is no standard mechanism to convert some ruby object back
into the source code. You'll have to parse the code yourself.

Kind regards

robert
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top