Tsallaka zuwa abun ciki
Justice Tech School

sort_by, min_by, group_by

sort_by, min_by, max_by and group_by work with a block.

Example
w = %w[pear fig apple kiwi]
puts w.sort_by(&:length).inspect
puts w.min_by(&:length), w.max_by(&:length)
p w.group_by(&:length)

Result

["fig", "pear", "kiwi", "apple"]
fig
apple
{4=>["pear", "kiwi"], 3=>["fig"], 5=>["apple"]}

Exercise

Group items.

w.(&:length)

Quiz Test

What does group_by return?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.