如何访问每个闭包中的父集合? [英] How to access the parent collection in a each closure?

查看:117
本文介绍了如何访问每个闭包中的父集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找替换 * parent * 的东西。

I'm looking from something that to replace the *parent*.

%w[apple apples].each do |w|
   next if *parent*.includes? w + "s"
   puts w
end

# output: apples


推荐答案

每个是一个约定,没有一个父集合特别是传递给每一个。只是命名,例如

each is a convention, there is no concept of a "parent collection" for blocks in general or ones passed to each in particular. Just name it, eg

(parent = %w[apple apples]).each do |w|
  next if parent.includes? w + "s"
  puts w
end

方法传递父级,

例如

module Each2
 def each2
   each { |elem| yield(self, elem) }
 end
end

include Each2

%w[apple apples].each2 do |parent, w|
  next if parent.includes? w + "s"
  puts w
end

这篇关于如何访问每个闭包中的父集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆