Ruby Basic Exercises: Retrieve the student information from a hash
Ruby Basic: Exercise-31 with Solution
Write a Ruby program to retrieve the total marks where subject name and marks of a student stored in a hash.
Sample subject and marks : Literature -74, Science – 89, Math-91
Ruby Code:
student_marks = Hash.new 0
student_marks['Literature'] = 74
student_marks['Science'] = 89
student_marks['Math'] = 91
total_marks = 0
student_marks.each {|key,value|
total_marks +=value
}
puts "Total Marks: "+total_marks.to_s
Output:
Total Marks: 254
Flowchart:
Ruby Code Editor:
Contribute your code and comments through Disqus.
Previous:Write a Ruby program to check two non-negative integer values and return true if they have the same last digit.
Next: Write a Ruby program to print a specified character twenty times.
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