Ruby - 未定义的方法“<”
Ruby - undefined method '<'
我正在学习有关如何将过程放入方法的教程。
他们问我以下问题:
"Now we will use the table ages. Create a variable called "young”并将调用“.select”的结果分配给 "ages",并放置您的 "inf_100" proc 参数以过滤年龄小于 100。不要忘记使用与号 (&) 块转换你的 proc。"
所以我做了如下操作,但我得到了以下结果
error : "undefined method `<' for [23, 101, 7, 104, 11, 94, 100, 121,
101, 70, 44]:Array"
我尝试了很多东西,但我总是犯这个错误,但我不知道哪里出了问题
ages = [23, 101, 7, 104, 11, 94, 100, 121, 101, 70, 44]
inf_100 = Proc.new { |i| i.select(&ages<100) } #
他们在之前的练习中问了我什么
jeunes = ages.select(&inf_100) #
当前练习
谁能告诉我哪里做错了?谢谢!
我假设上一章要求你写一个Proc
,它接受一个整数并判断它是否小于100。
应该这样写:
inf_100 = Proc.new {|i| i < 100 }
可以解释为:
inf_100 is assigned with a Proc
which takes i
as parameter and returns the result of i < 100
.
我正在学习有关如何将过程放入方法的教程。
他们问我以下问题:
"Now we will use the table ages. Create a variable called "young”并将调用“.select”的结果分配给 "ages",并放置您的 "inf_100" proc 参数以过滤年龄小于 100。不要忘记使用与号 (&) 块转换你的 proc。"
所以我做了如下操作,但我得到了以下结果
error : "undefined method `<' for [23, 101, 7, 104, 11, 94, 100, 121, 101, 70, 44]:Array"
我尝试了很多东西,但我总是犯这个错误,但我不知道哪里出了问题
ages = [23, 101, 7, 104, 11, 94, 100, 121, 101, 70, 44]
inf_100 = Proc.new { |i| i.select(&ages<100) } #
他们在之前的练习中问了我什么
jeunes = ages.select(&inf_100) #
当前练习
谁能告诉我哪里做错了?谢谢!
我假设上一章要求你写一个Proc
,它接受一个整数并判断它是否小于100。
应该这样写:
inf_100 = Proc.new {|i| i < 100 }
可以解释为:
inf_100 is assigned with a
Proc
which takesi
as parameter and returns the result ofi < 100
.