Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

212
Views
Compact for array two dimensions in Ruby

As we know compact in Array, it works like this :

[ "a", nil, "b", nil, "c", nil ].compact
              #=> [ "a", "b", "c" ]

Is there a function similar to do that for an array two dimentions ?

  [[nil, nil], ["Prestation Standard", 1]].compact => [["Prestation Standard", 1]]
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

This would work:

ary = [[nil, nil], ["Prestation Standard", 1]]

ary.map(&:compact).reject(&:empty?)
#=> [["Prestation Standard", 1]]

map(&:compact) returns a new 2D array with all nil values removed from the inner arrays and reject(&:empty?) removes those that are empty.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!