DEV Community

Discussion on: Code Golf: Conditionally Add To An Array

Collapse
foca profile image
Nicolás Sanguinetti

In the spirit of golfing, using filter_map (ruby 2.7+) would make the hash version a bit shorter:

def current_statuses
  {
    "in_contention" => under_par?,
    "past_the_turn" => back_nine?,
  }.filter_map { |status, condition| status if condition }
end
Enter fullscreen mode Exit fullscreen mode