It groups elements of a collection based on the result of a block, returning a hash where keys are the group criteria and values are arrays of elements in each group.
Click to reveal answer
beginner
How do you use group_by to categorize numbers as even or odd?
You pass a block that returns :even or :odd based on each number. For example: numbers.group_by { |n| n.even? ? :even : :odd }
Click to reveal answer
beginner
What type of object does group_by return?
It returns a Hash where each key is a group label and each value is an array of elements belonging to that group.
Click to reveal answer
intermediate
Can group_by be used on arrays of strings? Give an example.
Yes! For example, grouping words by their first letter: words.group_by { |word| word[0] } groups words by their starting character.
Click to reveal answer
beginner
What happens if the block passed to group_by returns the same value for multiple elements?
All those elements are grouped together under the same key in the resulting hash.
Click to reveal answer
What does group_by return when called on an array?
AAn array sorted by the block's return value
BA hash grouping elements by the block's return value
CA single element from the array
DA boolean indicating if grouping was successful
✗ Incorrect
group_by returns a hash where keys are group labels and values are arrays of grouped elements.
How would you group an array of numbers into 'positive' and 'negative' using group_by?
Option A correctly uses a block returning symbols :positive or :negative based on the number's sign.
If you group words by their length using group_by, what will the keys of the resulting hash be?
AThe length of each word
BThe first letter of each word
CThe words themselves
DThe last letter of each word
✗ Incorrect
Grouping by length means the keys are integers representing word lengths.
What happens if the block passed to group_by returns nil for some elements?
AThose elements are ignored
BThose elements are grouped under an empty string key
CAn error is raised
DThose elements are grouped under the <code>nil</code> key
✗ Incorrect
Elements with block result nil are grouped under the nil key in the hash.
Which of these is a valid use of group_by?
A[1,2,3,4].group_by
B[1,2,3,4].group_by(2)
C[1,2,3,4].group_by { |n| n % 2 }
D[1,2,3,4].group_by(&:to_s)
✗ Incorrect
Options C and D are valid uses of group_by: C uses a block to group numbers by remainder when divided by 2 (even/odd), and D uses a symbol to proc to group by string representation.
Explain how the group_by method works in Ruby and give a simple example.
Think about how you can sort items into labeled boxes based on a rule.
You got /3 concepts.
Describe a real-life situation where you could use group_by to organize data.
Imagine sorting your books or clothes into categories.
You got /3 concepts.
Practice
(1/5)
1. What does the Ruby method group_by do when called on an array?
easy
A. It converts the array into a string.
B. It groups elements into a hash based on a given condition.
C. It removes duplicate elements from the array.
D. It sorts the array in ascending order.
Solution
Step 1: Understand the purpose of group_by
The group_by method organizes elements by a condition and returns a hash.
Step 2: Compare with other array methods
Sorting, removing duplicates, or converting to string are different methods like sort, uniq, or join.
Final Answer:
It groups elements into a hash based on a given condition. -> Option B
Quick Check:
group_by = groups hash [OK]
Hint: Remember: group_by returns a hash of grouped elements [OK]
Common Mistakes:
Confusing group_by with sort or uniq
Expecting an array instead of a hash
Thinking it changes element order only
2. Which of the following is the correct syntax to group an array nums by even or odd using group_by?
easy
A. nums.group_by(n % 2 == 0)
B. nums.group_by do n % 2 == 0 end
C. nums.group_by { |n| n % 2 == 0 }
D. nums.group_by ->(n) { n % 2 == 0 }
Solution
Step 1: Recall block syntax for group_by
The method requires a block with curly braces or do-end and a block variable.
Step 2: Check each option's syntax
nums.group_by { |n| n % 2 == 0 } uses correct block with { |n| ... }. nums.group_by(n % 2 == 0) misses block. nums.group_by do n % 2 == 0 end lacks block variable. nums.group_by ->(n) { n % 2 == 0 } uses lambda incorrectly.
Which code correctly groups people by their age using group_by?
hard
A. people.group_by { |person| person[:age] }
B. people.group_by { |person| person.name }
C. people.group_by { |person| person['age'] }
D. people.group_by { |person| person.age }
Solution
Step 1: Understand data structure and access
Each element is a hash with symbol keys like :age, so access with person[:age].
Step 2: Check each option's key access
people.group_by { |person| person[:age] } uses correct symbol key access. people.group_by { |person| person.name } and D use dot notation which doesn't work on hashes. people.group_by { |person| person['age'] } uses string key which is incorrect here.
Final Answer:
people.group_by { |person| person[:age] } -> Option A