如何用更少的行写出这个条件?

时间:2015-11-18 12:28:12

标签: ruby-on-rails ruby

我在我的模型中写了这段代码:

percentage = 0
if self.date_of_birth.present?
  percentage += 15
end
if self.gender.present?
  percentage += 15
end
if self.relationship_status.present?
  percentage += 10
end
if self.language.present?
  percentage += 10
end
if self.qualification.present?
  percentage += 10
end
if self.interests.present?
  if self.interests.count >= 10
    percentage += 10
  else
    percentage += self.interests.count * 5
  end
end

但它看起来并不好。这是一个小东西的很多代码。我想减少行数。

7 个答案:

答案 0 :(得分:4)

你可以内联这样做:

percentage += 15 if self.date_of_birth.present?

而不是:

if self.interests.count >= 10
    percentage += 10
else
    percentage += self.interests.count*5
end

您可以使用三元运算符:

percentage += self.interests.count >= 10 ? 10 : self.interests.count*5

答案 1 :(得分:3)

percentage = [
  (15 if date_of_birth.present?),
  (15 if gender.present?),
  (10 if relationship_status.present?),
  (10 if language.present?),
  (10 if qualification.present?),
  ((counts = interests.count.to_i) >= 10 ? 10 : (counts * 5)),
].compact.sum

答案 2 :(得分:1)

您可以在模型中使用实例方法:

#app/models/model.rb
class Model < ActiveRecord::Base
   def percentage
      value = 0
      values = [[:date_of_birth, 15], [:gender, 15], [:relationship_status,10], [:language,10], [:qualification, 10]]
      values.each do |attr,val|
          value += val if self.send(attr).present?
      end
      value += self.interests.count >= 10 ? 10 : self.interests.count*5 if self.interests.present?
      # Rails should return the value of the last line, which is the "value" var
   end
end

这样您就可以使用@user.percentage,其中@user是模型的实例变量。

答案 3 :(得分:0)

就个人而言,我认为“少行”并不是一个好主意,但如果你想用较少的代码编写代码,可以这样写:

percentage = 0; if date_of_birth.present? then percentage += 15 end; if gender.present? then percentage += 15 end; if relationship_status.present? then percentage += 10 end; if language.present? then percentage += 10 end; if qualification.present? then percentage += 10 end; if interests.present? then if interests.count >= 10 then percentage += 10 else percentage += interests.count*5 end end

在Ruby中,您(几乎)总是可以用分号替换换行符,以使您的代码适合更少的行。事实上,每个 Ruby程序都可以始终写在一行上。

答案 4 :(得分:0)

inc_att = ["date_of_birth", "gender", "relationship_status" , "language", "qualification", "interests"]

inc_att.each do |s|

  if self[s].present? && (s == "date_of_birth" || s == "gender")
      percentage += 15
  elsif self[s].present? && s == "interests" && self[s].count < 10
      percentage += self[s].count * 5
  else
      percentage += 10 if self[s].present?
  end

end 

看看它

答案 5 :(得分:0)

inc_att = ["date_of_birth", "gender", "relationship_status" , "language",   "qualification", "interests"]

inc_att.each do |s|

if self[s].present? && (s == "date_of_birth" || s == "gender")
  percentage += 15
elsif self[s].present? && s == "interests" && self[s].count < 10
  percentage += self[s].count * 5
else
  percentage += 10 if self[s].present?
end

end 

答案 6 :(得分:0)

受@ sawa的回答启发:

counts = interests.count.to_i
percentage = (counts >= 10 ? 10 : (counts * 5)) + 
  [
    date_of_birth.present?       && 15,
    gender.present?              && 15,
    relationship_status.present? && 10,
    language.present?            && 10,
    qualification.present?       && 10,
  ].select(&:itself).sum