ruby should I use self. or @

前端 未结 4 1510
小蘑菇
小蘑菇 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:28

    self.lines is a method, @lines is the instance variable. In your constructor you'll want to use self.lines I would argue, but that's up for debate. It's just a stylistic difference, really. If you want a deeper discussion of direct vs. indirect variable access, read the chapter from Kent Beck's Smalltalk Best Practice Patterns.

提交回复
热议问题