lambda中的参数数量错误

时间:2021-06-22 18:56:27

In my sinatra app I have the code of

在我的sinatra应用程序中,我有代码

haml :"page123", locals:
      {
        items: (my_data.sort ->(item1, item2){ item2[:date] <=> item1[:date]}), 
      }, 
      layout: need_layout?

What I need to do is to sort my_data by date in descending order. However, I' getting the error here

我需要做的是按日期降序排序my_data。但是,我在这里得到错误

wrong number of arguments(1 for 0)

What did I do wrong?

我做错了什么?

1 个解决方案

#1


3  

Enumerable#sort does not take any arguments. You are essentially passing a Lambda object as the first and only argument to the sort method. What you can do is supply a block to be used to sort the collection.

可枚举#s排序不带任何参数。您实际上是将Lambda对象作为sort方法的第一个也是唯一的参数传递。您可以做的是提供一个用于对集合进行排序的块。

wrong number of arguments(1 for 0) is saying that the method was given 1 argument and expected 0.

错误的参数数量(1表示0)表示该方法被赋予1个参数且预期为0。

Try this:

尝试这个:

my_data.sort{ |a, b| b[:date] <=> a[:date] }

#1


3  

Enumerable#sort does not take any arguments. You are essentially passing a Lambda object as the first and only argument to the sort method. What you can do is supply a block to be used to sort the collection.

可枚举#s排序不带任何参数。您实际上是将Lambda对象作为sort方法的第一个也是唯一的参数传递。您可以做的是提供一个用于对集合进行排序的块。

wrong number of arguments(1 for 0) is saying that the method was given 1 argument and expected 0.

错误的参数数量(1表示0)表示该方法被赋予1个参数且预期为0。

Try this:

尝试这个:

my_data.sort{ |a, b| b[:date] <=> a[:date] }