Ahkshaey Ravi Ahkshaey Ravi -4 years ago 119
Ruby Question

Simplify if else condition ruby

I have a if else condition in my ruby code listed below:

if !category.nil?
return false unless company_matches.any? { |w|
comparison = /(\s|^)#{w}(\s|$)/i
(title.index(comparison) || description.index(comparison) || clean_title.index(comparison) || clean_desc.index(comparison)) && (category == 'Business')}
else
return false unless company_matches.any? { |w|
comparison = /(\s|^)#{w}(\s|$)/i
(title.index(comparison) || description.index(comparison) || clean_title.index(comparison) || clean_desc.index(comparison))}
end


How can i simplify this to make it look more subtle?

Answer Source
company_matches.any? do |company|
  [title, description, clean_title, clean_desc].any? do |attribute|
    attribute.match? /(\s|^)#{company}(\s|$)/i
  end && (category == 'Business' || category.nil?)
end
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download