Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8469499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:14:44+00:00 2026-06-10T16:14:44+00:00

There are some arrays in Ruby (is this case there 4 array) array1 =

  • 0

There are some arrays in Ruby (is this case there 4 array)

array1 = [{key="label1.1", value="label1.2"}, {key="label1.2", value="label1.2"}]
array2 = [{key="label2.1", value="label2.2"}]

array3 = []
array4 = nil
result_array = array1 | array2 | array3 | array4 

Each of arrays has the same structure as others: it’s hash values. How many elements in it, if it’s nil or empty – it’s not known.

So I need result_array to look:

[{key="label1.1", value="label1.2"}, {key="label1.2", value="label1.2"}, {key="label2.1", value="label2.2"}]

However that code is going to cause an exception because array4 is equal to nil.

Is there any, Ruby, way to check if an array is not nil and not empty and if so, then merge it to result_array?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T16:14:46+00:00Added an answer on June 10, 2026 at 4:14 pm
    [array1, array2, array3, array4].compact.reduce([], :|)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just read this answer Converting upper-case string into title-case using Ruby . There
I'm trying to us Async.js to process an array of items. Is there some
I have a Ruby array containing some string values. I need to: Find all
I am trying to learn Ruby, and arrays are giving me some trouble. I
Is there some sort of Ruby to_s method that changes a variable to a
In Ruby, there are some filter functions that produce a different type than what
If I want to interleave a set of arrays in Ruby, and each array
I have a very simple Ruby question. Is there a way to refactor this:
Are there some good resources tutorials or anyone has tried to implement a Capcha
Is there some way to define an abstract type as a parameter in an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.