ruby should I use self. or @

前端 未结 4 1508
小蘑菇
小蘑菇 2021-01-30 17:57

Here is my ruby code

class Demo
  attr_accessor :lines

  def initialize(lines)
    self.lines = lines
  end
end

In the above code I could have

4条回答
  •  醉酒成梦
    2021-01-30 18:25

    The main difference would be if you redefined lines= to do something other than @lines = lines.

    For example, you could add validation to the attribute (for example, raising if lines is empty).

提交回复
热议问题