Ruby Array Exercises: Create a new array with the first element of two arrays
Ruby Array: Exercise-28 with Solution
Write a Ruby program to create a new array with the first element of two arrays. If lenght of any array is 0, ignore that array.
def check_array(a, b) front =  if(a.length > 0 && b.length > 0) front = a front = b elsif (a.length > 0) front = a elsif (b.length > 0) front = b end return front end print check_array([3, 4, 5, 6], [7, 3, 4]),"\n" print check_array([3, 4, 5], [6, 7, 3, 4, 7]),"\n" print check_array([3, 4], )
[3, 7] [3, 6] 
Ruby Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a Ruby program to create a new array using first three elements of an given array of integers. If the length of the given array is less than three return the original array.
Next: Write a Ruby program to get the number of even integers in a given array.
What is the difficulty level of this exercise?
- New Content published on w3resource:
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework