gpt4 book ai didi

ruby - Ruby 中的双管道是什么(不是 ||= )?

转载 作者:太空宇宙 更新时间:2023-11-03 17:50:45 25 4
gpt4 key购买 nike

任何人都可以向我解释一下双管道 |i| 在这个例子中代表什么或被称为什么?

(1..10).detect {|i| (1..10).include?(i * 3)}

最佳答案

这是在 Ruby 中声明 block 参数的语法。

阅读Cycling and Looping—a.k.a. Iteration

Let’s look at that iterator in more depth:

@names.each do |name|
puts "Hello #{name}!"
end

#each is a method that accepts a block of code then runs that block of code for every element in a list, and the bit between do and end is just such a block. A block is like an anonymous function or lambda. The variable between pipe characters is the parameter for this block.

关于ruby - Ruby 中的双管道是什么(不是 ||= )?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25273115/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com