Ruby Array Exercises: Create a new array with the elements in reverse order
Ruby Array: Exercise-12 with Solution
Write a Ruby program to create a new array with the elements in reverse order from a given array of integers length 3.
Ruby Code:
def check_array(nums)
reversed = nums[2], nums[1], nums[0]
return reversed;
end
print check_array([1, 2, 5]),"\n"
print check_array([1, 2, 3]),"\n"
print check_array([1, 2, 4])
Output:
[5, 2, 1] [3, 2, 1] [4, 2, 1]
Flowchart:
Ruby Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a Ruby program to create an array with the elements "rotated left" of a given array of integers length 3.
Next: Write a Ruby program to find the larger between the first and last elements of a given array of integers and length 3. Replace all the other values to be that value. Return the changed array.
What is the difficulty level of this exercise?
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- 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
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework