In ruby you can internally access variables directly via @var_name
or via private getters attr_reader :var_name
.
Which solution is more (semantically?) correct? Any advantages/disadvantages of using either solution 1 or solution 2?
Solution 1:
class Point
def initialize(x, y)
@x = x
@y = y
end
def distance
Math.sqrt(@x ** 2 + @y ** 2)
end
end
Solution 2:
class Point
def initialize(x, y)
@x = x
@y = y
end
def distance
Math.sqrt(x ** 2 + y ** 2)
end
private attr_reader :x, :y
end
Aucun commentaire:
Enregistrer un commentaire