我如何使我简单的Ruby类更加简洁,看起来很麻烦?

时间:2018-08-23 15:08:30

标签: ruby

我可以以某种方式使用Struct或OpenStruct使我的课程更简洁吗?

class RecipientScorer::ScoreResult

  attr_accessor :id, :score_data, :total_score, :percent_match

  def initialize(id, score_data, total_score, percent_match)
    @id = id
    @score_data = score_data
    @total_score = total_score
    @percent_match = percent_match
  end

end

1 个答案:

答案 0 :(得分:2)

您的问题包含答案-使用Struct

RecipientScorer::ScoreResult = Struct.new(
  :id,
  :score_data,
  :total_score,
  :percent_match
)