我有这样的代码:
default_group_id = @group_list[0].list[0].name但是有时@group_list[0]的列表成员是空的,所以我的代码会崩溃:),所以我需要找到它的列表成员不是零的第一个@group_list[i],并使用它。我们怎么能这么做?
结构如下:

发布于 2013-08-26 16:10:23
您可以使用Enumerable#find
@group_list.find { |x| !x["list"].blank? }
#=> first non-nil and non-empty list in group_list发布于 2016-10-06 19:40:57
具有传递Enumerable#find的Object#itself是一个方便的快捷方式:
@group_list.find(&:itself)发布于 2013-08-26 16:12:21
你可以用Enumerable#find
@group_list.find{|x|!x.nil?} # => the first non-nil element in @group_listhttps://stackoverflow.com/questions/18448331
复制相似问题