DEV Community

Discussion on: Code Golf: Conditionally Add To An Array

Collapse
fwolfst profile image
Felix Wolfsteller • Edited on

With addition?

def current_statuses
  # this could also be done using inject
  [] +
  (under_par? ? %w{in_contention} : []) +
  (back_nine? ? %w{past_the_turn} : [])
end
Enter fullscreen mode Exit fullscreen mode

(or use ['in_contention'] instead of %w{in_contention}).

Or with dynamic evaluation

def statuses
  status_evals = {
    :under_par? => 'in_contention',
    :back_nine? => 'past_the_turn'
  }

  # could obviously be shortend by chaining
  status_evals.select{|k,_v| method(k).call}.values
end
Enter fullscreen mode Exit fullscreen mode