w3resource

Ruby Array Exercises: Create a new array from two array of integers

Ruby Array: Exercise-23 with Solution

Write a Ruby program to create a new array of length 4 containing all their elements from two array of integers, length 2.

Ruby Array Exercises: Create a new array from two array of integers

Ruby Code:

def check_array(a, b)
   arr1 = a[0], a[1], b[0], b[1]
   return arr1;
end
print check_array([1, 3], [5, 4]),"\n"
print check_array([11, 3], [5, 21])  

Output:

[1, 3, 5, 4]
[11, 3, 5, 21]

Flowchart:

Flowchart: Create a new array from two array of integers

Ruby Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Ruby program to create a new array of length 2 containing the middle two elements from an given array of integers of even length 2 or more.
Next: Write a Ruby program to swap the first and last elements of an given array of integers, length will be at least 1. Return the modified array.

What is the difficulty level of this exercise?