Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I make this massive Ruby if/elsif statement more compact and cleaner?
    text
    copied!<p>The following if/elsif statement is clearly a behemoth. The purpose of it is to change the phrasing of some text based on if certain data has been filled in by the user. I feel like there's got to be a better way to do this without taking up 30+ lines of code, but I'm just not sure how since I'm trying to customize the text pretty significantly based on the data available.</p> <pre><code>if !birthdate.blank? &amp;&amp; !location.blank? &amp;&amp; !joined.blank? &amp;&amp; !death.blank? "&lt;p class='birthinfo'&gt;#{name} was born on #{birthdate.strftime("%A, %B %e, %Y")} in #{location}. #{sex} passed away on #{death.strftime("%B %e, %Y")} at the age of #{calculate_age(birthdate, death)}. #{sex} was a member of #{link_to user.login, profile_path(user.permalink)}'s family for #{distance_of_time_in_words(joined,death)}.&lt;/p&gt;" elsif !birthdate.blank? &amp;&amp; !location.blank? &amp;&amp; !joined.blank? &amp;&amp; death.blank? "&lt;p class='birthinfo'&gt;#{name} was born on #{birthdate.strftime("%A, %B %e, %Y")} in #{location} and is #{time_ago_in_words(birthdate)} old. #{sex} has been a member of #{link_to user.login, profile_path(user.permalink)}'s family for #{time_ago_in_words(joined)}.&lt;/p&gt;" elsif birthdate.blank? &amp;&amp; !location.blank? &amp;&amp; !joined.blank? &amp;&amp; !death.blank? "&lt;p class='birthinfo'&gt;#{name} was born in #{location}. #{sex} passed away on #{death.strftime("%B %e, %Y")}. #{sex} was a member of #{link_to user.login, profile_path(user.permalink)}'s family for #{distance_of_time_in_words(joined,death)}.&lt;/p&gt;" elsif birthdate.blank? &amp;&amp; !location.blank? &amp;&amp; !joined.blank? &amp;&amp; death.blank? "&lt;p class='birthinfo'&gt;#{name} was born in #{location}. #{sex} has been a member of #{link_to user.login, profile_path(user.permalink)}'s family for #{time_ago_in_words(joined)}.&lt;/p&gt;" elsif birthdate.blank? &amp;&amp; location.blank? &amp;&amp; !joined.blank? &amp;&amp; !death.blank? "&lt;p class='birthinfo'&gt;#{name} was a member of #{link_to user.login, profile_path(user.permalink)}'s family for #{distance_of_time_in_words(joined,death)}. #{sex} passed away on #{death.strftime("%B %e, %Y")}.&lt;/p&gt;" elsif birthdate.blank? &amp;&amp; location.blank? &amp;&amp; !joined.blank? &amp;&amp; death.blank? "&lt;p class='birthinfo'&gt;#{name} has been a member of #{link_to user.login, profile_path(user.permalink)}'s family for #{time_ago_in_words(joined)}.&lt;/p&gt;" elsif !birthdate.blank? &amp;&amp; location.blank? &amp;&amp; !joined.blank? &amp;&amp; !death.blank? "&lt;p class='birthinfo'&gt;#{name} was born on #{birthdate.strftime("%A, %B %e, %Y")}. #{sex} passed away on #{death.strftime("%B %e, %Y")} at the age of #{calculate_age(birthdate, death)}. #{sex} was a member of #{link_to user.login, profile_path(user.permalink)}'s family for #{distance_of_time_in_words(joined,death)}.&lt;/p&gt;" elsif !birthdate.blank? &amp;&amp; location.blank? &amp;&amp; !joined.blank? &amp;&amp; death.blank? "&lt;p class='birthinfo'&gt;#{name} was born on #{birthdate.strftime("%A, %B %e, %Y")} and is #{time_ago_in_words(birthdate)} old. #{sex} has been a member of #{link_to user.login, profile_path(user.permalink)}'s family for #{time_ago_in_words(joined)}.&lt;/p&gt;" elsif !birthdate.blank? &amp;&amp; !location.blank? &amp;&amp; joined.blank? &amp;&amp; !death.blank? "&lt;p class='birthinfo'&gt;#{name} was born on #{birthdate.strftime("%A, %B %e, %Y")} in #{location}. #{sex} passed away on #{death.strftime("%B %e, %Y")} at the age of #{calculate_age(birthdate, death)}. #{sex} was a member of #{link_to user.login, profile_path(user.permalink)}'s family.&lt;/p&gt;" elsif !birthdate.blank? &amp;&amp; !location.blank? &amp;&amp; joined.blank? &amp;&amp; death.blank? "&lt;p class='birthinfo'&gt;#{name} was born on #{birthdate.strftime("%A, %B %e, %Y")} in #{location} and is #{time_ago_in_words(birthdate)} old. #{sex} is a member of #{link_to user.login, profile_path(user.permalink)}'s family.&lt;/p&gt;" elsif !birthdate.blank? &amp;&amp; location.blank? &amp;&amp; joined.blank? &amp;&amp; !death.blank? "&lt;p class='birthinfo'&gt;#{name} was born on #{birthdate.strftime("%A, %B %e, %Y")}. #{sex} passed away on #{death.strftime("%B %e, %Y")} at the age of #{calculate_age(birthdate, death)}. #{sex} was a member of #{link_to user.login, profile_path(user.permalink)}'s family.&lt;/p&gt;" elsif !birthdate.blank? &amp;&amp; location.blank? &amp;&amp; joined.blank? &amp;&amp; death.blank? "&lt;p class='birthinfo'&gt;#{name} was born on #{birthdate.strftime("%A, %B %e, %Y")} and is #{time_ago_in_words(birthdate)} old. #{sex} is a member of #{link_to user.login, profile_path(user.permalink)}'s family.&lt;/p&gt;" elsif birthdate.blank? &amp;&amp; !location.blank? &amp;&amp; joined.blank? &amp;&amp; !death.blank? "&lt;p class='birthinfo'&gt;#{name} was born in #{location}. #{sex} passed away on #{death.strftime("%B %e, %Y")}. #{sex} was a member of #{link_to user.login, profile_path(user.permalink)}'s family.&lt;/p&gt;" elsif birthdate.blank? &amp;&amp; !location.blank? &amp;&amp; joined.blank? &amp;&amp; death.blank? "&lt;p class='birthinfo'&gt;#{name} was born in #{location}. #{sex} is a member of #{link_to user.login, profile_path(user.permalink)}'s family.&lt;/p&gt;" else "&lt;p class='birthinfo'&gt;#{name} is a member of #{link_to user.login, profile_path(user.permalink)}'s family.&lt;/p&gt;" end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload