
At the “Welcome to Ruby” house party, enumerables are the scensters doing ketamine in the upstairs master bedroom.
Elusive and full of deception, I have no interest in getting to know them. They all dress in their own special syntax, for no discernible reason — except maybe for the sake of making some inane statement. But after a couple of these parties and against my better judgement, I get to know .each; our moms are friends and so I am delicately hand-held through an artificial friending process.
array = [1.0, 2.0, 3.0]
def conver_to_integer(array) array.each do |x|
puts x.to_i
endend
1
2
3=> [1.0, 2.0, 3.0]
.each is nice enough, it turns out. She’s steady, consistent, always returning the same original array. And she introduces me to her cousins .collect, .map, and .select.

.collect and .map are identical twins who have the cloying habit of insisting on being treated as unique individuals, all the while presenting no notable discrepancies in personality.
array = [1.0, 2.0, 3.0]
def convert_to_integer_with_collect(array) array.collect do |x|
puts x.to_i
endend
1
2
3=> [1, 2, 3]____________________________________________________________________
def convert_to_integer_with_map(array) array.map do |x|
puts x.to_i
endend
1
2
3=> [1, 2, 3]____________________________________________________________________
#wtf
At one of the usual parties, variables litter the floor and the stench of discarded decimal points (of floats, now converted integers) wreak havoc on my senses. The drinks are stronger than I thought. I need to eat something. And who’s guarding the snack table but .select’s aloof friend .max_by…

We’ve never spoken. He attends these things infrequently enough that I’ve managed to skid by without making his acquaintance.
But the snacks.
He’s guarding so many snacks.
In past snack pursuits, I’ve recruited the collective help of .collect, .length, .index, and .max to get what I want. These were clumsy schemes at best:
array = ["short", "longer", "longest"]
def find_longest_string(array)
all_string_lengths = array.collect do |string|
string.length
end
index_of_longest_string =
all_string_lengths.index(all_string_lengths.max) longest_string = array[index_of_longest_string]end=> "longest"
All this trouble just to avoid ever having to interact with .max_by.
But there’s something about him tonight. Maybe it’s how he styled his underscore. Maybe I’m just drunk. Either way. It’s happening.
array = ["short", "longer", "longest"]def find_longest_string(array)
array.max_by do |string|
string.length
end
end=> "longest"
.max_by was not the reputation that preceded him. He was so much more. Down to earth. Elegant. Straight forward. Returning the single element that gave the maximum value from the given block. He stored the return value of the given block for each item in the collection, then did all the comparison sorting on those return values directly.
And if no block was given, he returned an enumerator instead. I was in love.
We hooked up that night.
It was anything but clumsy.
___________________________________________________________________
note: not condoning the recreational use of ketamine.
