Ruby Array Exercises: Convert an array into an index hash
Ruby Array: Exercise-42 with Solution
Write a Ruby program to convert an array into an index hash.
Ruby Code:
nums = [10, 20, 30, 40]
print "Original array:\n"
print nums
print "\nIndex Hash:\n"
print Hash[nums.zip]
Output:
Original array: [10, 20, 30, 40] Index Hash: {10=>nil, 20=>nil, 30=>nil, 40=>nil}
Flowchart:
Ruby Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a Ruby program to check whether the value 2 appears in an given array of integers exactly 2 times, and no 2's are next to each other.
Next: Write a Ruby program to find most occurred item in an given 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