How does Array#sort work when a block is passed?

前端 未结 7 1702
北海茫月
北海茫月 2020-11-29 18:10

I am having a problem understanding how array.sort{ |x,y| block } works exactly, hence how to use it?

An example from Ruby documentation:



        
7条回答
  •  鱼传尺愫
    2020-11-29 18:43

    Some miscellaneous points:

    • x and y are called block parameters. The sort method basically says "I'll give you x and y, you determine whether x or y should come first, and I'll look after the boring stuff with regards to sorting"
    • <=> is called a spaceship operator.

提交回复
热议问题