โ Ruby Programming learning pathRuby Programming ยท Module 5 of 10
Arrays and hashes
Arrays are ordered lists; hashes are key-value pairs.
๐ Key ideas
- Arrays: marks = [72, 85, 90]; marks[0] is 72; marks.last is 90.
- push or << adds items; pop removes the last one.
- Hashes: student = { name: "Asha", city: "Erode" }.
- Access with student[:name]; add with student[:age] = 20.
- Both have many built-in methods: size, include?, sort, keys, values.
๐ก Real-life example: An array is a numbered queue of students; a hash is the student's ID card with named fields.
ArrayOrdered list of values
HashKey-value pairs
<<Append an item to an array
keysAll keys of a hash