简体   繁体   中英

How can I write this conditional in fewer lines?

I wrote this code in my model:

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

But it does not look good. It is a lot of code for a small thing. I want to reduce the number of lines.

You can do it inline, like this:

percentage += 15 if self.date_of_birth.present?

Instead of this:

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

You can use a ternary operator:

percentage += self.interests.count >= 10 ? 10 : self.interests.count*5
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

You could use an instance method in your model:

#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

This would allow you to use @user.percentage , where @user is your instance var for the model.

Personally, I don't think that "less lines" is a good idea, but if you want your code in less lines, you can write it like this:

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

In Ruby, you can (almost) always replace linebreaks with semicolons to make your code fit on less lines. In fact, every Ruby program can always be written on a single line.

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 

Have a look into it

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 

Inspired by @sawa's answer:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM