What is the difference between Methods and Attributes in Ruby?

后端 未结 5 633
抹茶落季
抹茶落季 2021-01-31 10:11

Can you give me an example?

5条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-31 11:07

    Attributes are specific properties of an object. Methods are capabilities of an object.

    In Ruby all instance variables (attributes) are private by default. It means you don't have access to them outside the scope of the instance itself. The only way to access the attribute is using an accessor method.

    class Foo
      def initialize(color)
        @color = color
      end
    end
    
    class Bar
      def initialize(color)
        @color = color
      end
    
      def color
        @color
      end
    end
    
    class Baz
      def initialize(color)
        @color = color
      end
    
      def color
        @color
      end
    
      def color=(value)
        @color = value
      end
    end
    
    f = Foo.new("red")
    f.color # NoMethodError: undefined method ‘color’
    
    b = Bar.new("red")
    b.color # => "red"
    b.color = "yellow" # NoMethodError: undefined method `color=' 
    
    z = Baz.new("red")
    z.color # => "red"
    z.color = "yellow"
    z.color # => "yellow"
    

    Because this is a really commmon behavior, Ruby provides some convenient method to define accessor methods: attr_accessor, attr_writer and attr_reader.

提交回复
热议问题