Code Golf: Conditionally Add To An Array

Engineering Insights

#
Min Read
Published On
March 13, 2025
Updated On
March 24, 2025
Code Golf: Conditionally Add To An Array

The Grass Is Always Greener

We're building a system to track a golfer's statuses during a tournament. This isa competitive tournament with people who are much better than I will ever be, soif a golfer is currently scoring under par, they're in contention to win. On this 18 hole course, if they've played the first nine holes, they've made the turn.Let's explore a number of ways we can build up an array that keeps track ofwhich, if any, of these statuses a particular golfer qualifies for. Not content to settle for one that works, we'll dig into a variety of options.

Teeing Up An Option

We can start with an empty array, and explicitly add in any of the statuses thatthe golfer meets the conditions for.

def current_statuses
  statuses = []

  if under_par?
    statuses << "in_contention"
  end

  if back_nine?
    statuses << "past_the_turn"
  end

  statuses
end

There's nothing tremendously exciting here, and that's not a bad thing! It'sreasonably clear what this is doing.

Tapping It In

We can make the prior suggestion a little more terse by using tap.

def current_statuses
  [].tap do |statuses|
    if under_par?
      statuses << "in_contention"
    end

    if back_nine?
      statuses << "past_the_turn"
    end
  end
end

This eliminates the need for the statuses temporary array from the priorsection.

Taking a Compact Swing

We can also build our array to have an entry for each of the conditionals wehave, and removing the ones that aren't relevant.

def current_statuses
  [
    under_par? ? "in_contention" : nil,
    back_nine? ? "past_the_turn" : nil,
  ].compact
end

By using compact,we'll remove any nil values - and we'll take advantage of that functionalityby explicitly adding nil if the golfer doesn't meet that condition.

Working On Your Backswing Takeaway

Speaking of taking things away, we can also do the opposite of the first approach. We'll start by having each status, and then removing the ones thatdo not meet the necessary condition.

def current_statuses
  statuses = ["in_contention", "past_the_turn"]

  unless under_par?
    statuses -= ["in_contention"]
  end

  unless back_nine?
    statuses -= ["past_the_turn"]
  end

  statuses
end

This may be more helpful in situations where there are a lot more statuses, andonly a few of them may need to be removed for certain reasons. It may also helpwhen the full list of statuses persists on its own elsewhere, but then you alsoneed a subset of them in a particular case.

Selecting The Right Club

Lastly, we'll put together a hash keyed on the statuses where the value is the result of the conditions. We can then flex some familiarity with Ruby's Enumerable module to pick out the applicable sections of the hash, returning only the statuses.

def current_statuses
  {
    "in_contention" => under_par?,
    "past_the_turn" => back_nine?,
  }.select { |_status, condition| condition }.keys
end

Similar to the prior suggestion, this may be helpful when you want to have thefull list of statuses and their associated conditions all compiled in one place,but then want to peel off which are relevant for a particular golfer at thistime.

Asking Help From The Caddie

Here are some of the ways that we might solve this problem. What other wayscould we build this functionality? Let me knowwhat other approaches you would take.

Learn more about how The Gnar builds Ruby on Rails applications.

Author headshot
Written by
, The Gnar Company

Related Insights

See All Articles
Engineering Insights
Context-Driven Development: The AI-First Alternative to Agile

Context-Driven Development: The AI-First Alternative to Agile

Context-Driven Development (CDD) is a software development methodology designed for AI-assisted coding. Learn how CDD differs from Agile and why detailed requirements are now the source code of the future.
Product Insights
How to Choose the Right Software Development Partner in 2026

How to Choose the Right Software Development Partner in 2026

Avoid project failure and costly delays. Learn how to choose the right software development partner in 2026 with our guide to vetting quality, teams, and warranties.
News
Expert Software Development Consulting Services

Expert Software Development Consulting Services

Been burned by agencies that over-promised and under-delivered? The Gnar offers guaranteed outcomes, fixed pricing, and a 12-month bug-free warranty. 100% US-based senior engineers.
Previous
Next
See All Articles