Determining method's visibility on the fly

后端 未结 3 1671
执笔经年
执笔经年 2021-01-19 02:45

I am writing a method that will define an instance method inside a class; something similar to attr_accessor:

class Foo
  custom_method(:foo)
end
         


        
3条回答
  •  广开言路
    2021-01-19 03:26

    I think this is impossible, because the scope visibility level set by Module.private is managed at the C virtual machine level and not exposed to Ruby.

    EDIT: and it's only available in the same syntactical scope that it is called, so when you call custom_method it loses the visibility level set inside the class declaration.

    It's set in set_visibility(), and used in vm_define_method(), but I can't find any reference to the corresponding variable being available from Ruby.

    I suggest using some kind of custom parameter to specify the visibility level of your methods.

提交回复
热议问题