Have following example of code:
%w{ Ruby C APL Perl Smalltalk}.min {|a,b| a.size <=> b.size}
return "C"
Can you explain me why "C"? What makes an operator "<=>" ?
Have following example of code:
%w{ Ruby C APL Perl Smalltalk}.min {|a,b| a.size <=> b.size}
return "C"
Can you explain me why "C"? What makes an operator "<=>" ?
The Spaceship operator returns -1 if the left is less than the right, 0 if they are equal, and 1 if the left is greater than the right.
In this case, you're comparing the length of each word in the array and returning the shortest word. If you removed .size
from the variable in the block, it would instead return the first word that occurs alphabetically (i.e. 'APL').