Ruby Singleton methods for class and objects

后端 未结 3 390
别那么骄傲
别那么骄傲 2021-02-02 12:19

I am learning Ruby Singletons and i found some ways to define and get list of Class and Object singleton methods.

Class Singleton Methods

Ways to define

3条回答
  •  一向
    一向 (楼主)
    2021-02-02 12:27

    Object singleton methods

    instance_eval

    class A
    end
    
    a = A.new
    
    a.instance_eval do
    def v 
    "asd"
    end
    end
    
     a.singleton_methods
     => [:z, :v] 
    

提交回复
热议问题