Attr_accessor on class variables

前端 未结 6 1740
Happy的楠姐
Happy的楠姐 2020-12-23 18:57

attr_accessor does not work on the following code. The error says \"undefined method \'things\' for Parent:Class (NoMethodError)\":



        
6条回答
  •  眼角桃花
    2020-12-23 19:24

    Аlso note that a singleton method is a method only for a single object. In Ruby, a Class is also an object, so it too can have singleton methods! So be aware of when you might be calling them.

    Example:

    class SomeClass
      class << self
        def test
        end
      end
    end
    
    test_obj = SomeClass.new
    
    def test_obj.test_2
    end
    
    class << test_obj
      def test_3
      end
    end
    
    puts "Singleton methods of SomeClass"
    puts SomeClass.singleton_methods
    puts '------------------------------------------'
    puts "Singleton methods of test_obj"
    puts test_obj.singleton_methods
    

    Singleton methods of SomeClass

    test


    Singleton methods of test_obj

    test_2

    test_3

提交回复
热议问题