在 ruby?? 中,我可以將方法系結到接收器(實體):
class TEST
def meth
end
end
bound_method = TEST.new.method(:meth)
問題:我怎樣才能從系結方法中恢復接收者?
可能的解決方案,在檔案中找到:
/*
* call-seq:
* binding.receiver -> object
*
* Returns the bound receiver of the binding object.
*/
static VALUE
bind_receiver(VALUE bindval)
{
const rb_binding_t *bind;
GetBindingPtr(bindval, bind);
return vm_block_self(&bind->block);
}
uj5u.com熱心網友回復:
我如何才能從系結方法中恢復接收者?
如果你看一下的檔案Method,你會發現方法Method#receiver,它回傳物件的系結接收者Method:
receiver→object回傳方法物件的系結接收者。
(1..3).method(:map).receiver # => 1..3
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/510047.html
標籤:红宝石
