Bird
0
0

What does the keys method do when called on a Ruby hash?

easy📝 Conceptual Q11 of 15
Ruby - Hashes
What does the keys method do when called on a Ruby hash?
ADeletes all keys from the hash
BReturns an array of all the values in the hash
CReturns an array of all the keys in the hash
DIterates over each key-value pair in the hash
Step-by-Step Solution
Solution:
  1. Step 1: Understand the keys method

    The keys method extracts all keys from the hash and returns them as an array.
  2. Step 2: Compare with other methods

    values returns values, each iterates pairs, so only keys returns keys array.
  3. Final Answer:

    Returns an array of all the keys in the hash -> Option C
  4. Quick Check:

    keys = keys array [OK]
Quick Trick: Remember: keys method lists all keys as an array [OK]
Common Mistakes:
  • Confusing keys with values method
  • Thinking keys modifies the hash
  • Mixing up keys with each method

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes