Context : I'm trying to insert a decorator pattern in Ruby as a decorator All underlying objects should be handed over to unknown methods, I used the Delaater Class. I could use SimpleDelegator, but I wanted to fully understand what I was doing. Then the original code I came with was: Which looks like the implementation of a simple operator is similar. But what I did not want to do was to show that the decorator to deal with coding shows that it is a decorator. I need full transparency. So I tinkered a little bit and this Coming together: In this way, I have the This is the thing: I do not understand why me Where did those two methods come from? If they were not implemented at all, then why did I need to define them? I think I must remember something about Ruby's object model or some things. Thank you. You can get a hint by observing the method: The owner of the law is That module is again between , in which Note that the Object is collected in kernel module, making kernel functions accessible to a global level. Although the example methods of the object are defined by the kernel module, we have selected them for documentation here for clarity.
class decorator & lt; Daylight DF initialization (component) super @component = component end def __setobj __ (o); @community = o & df __getobj__; Send @Comment and Diff (S, A); __SEND __ (S, * A) End End
Decorator.New (object.New) .class back
decorator
class decorator & lt; Daylight undef_method: == undef_method: class underf_moth: instance_of? # Decorated Object DF Initialize (Component) Super @Connant = Component Eddie Diff __setobj __ (O); @community = o & df __getobj__; Send @Comment and Diff (S, A); __Send __ (S, * A) End End
class or
example_of on my security object? Through this method, the underlying object will be sent through method_missing (which is implemented by the deliator).
: class
: instance_of . I can see that BasicObject defines
: == , so I had to define it, but what about those two? I saw a little bit in Basic Object Documentation and C code but nothing was found. I looked at the representative's document and code the same, and found nothing. It seems that the deliiter contains the kernel module, but the kernel # class or kernel # installation_off? Does not exist.
Decorator.instance_method (: class) # = & gt; # & Lt; Unbound Method: Decorator (# & lt; Module: 0x00000102137498 & gt;) # Class & gt;
decorator but in reality the
# & lt; Module: 0x00000102137498 & gt; . So there is an anonymous module which defines it interesting ... Let's see:
Decorator. Nosters # = & gt; [Decorator, Delegator, # & lt; Module: 0x00000102137498 & gt; BasicObject]
delegator and
BasicObject . So the
deliater does not directly obtain
from the default object . If you find the source code in
lib / delegate.rb you find:
class representative & lt; BasicObject kernel = :: Kernel.dup kernel.class_eval Do [: to_s, Inspection ,: = ~,:! ~ ,: === ,: & lt; = & Gt;,: eql ?,: hash] .each do | Meter | Undef_method m Finally a kernel copy of the
kernel module has been created in the kernel # ...
to_s
Inspect , etc ... but still
class and
example_of? is. It is included in the
deliq and this is where it comes from.
object to the
kernel module (but also includes the full module):
42. Method (: class) # = & gt; # & Lt; Method: Fiction (Kernel) # Class & gt; It is stated in:
Comments
Post a Comment