alm*_*lmo 0 ruby-on-rails ruby-on-rails-5
在我的 Rails 5 应用程序中,我有这样的内容:
a = [1,2,3]
a.map do |entry|
entry.delete if condition == true
end
Run Code Online (Sandbox Code Playgroud)
如果条件为真,则删除该条目。
现在我有这个:
a = [[1,2],[2,3],[3,4]]
a.map do |entry|
entry.delete if condition == true
end
Run Code Online (Sandbox Code Playgroud)
这循环遍历 a 但现在entry是一个数组,删除应该删除整个数组entry,但我得到:
wrong number of arguments (given 0, expected 1)
Run Code Online (Sandbox Code Playgroud)
有谁知道我如何循环遍历数组并删除整个子数组?
尝试这个:
a.delete_if {condition}
例如:
a = [[1,2],[2,3],[3,4]]
a.delete_if {|entry| entry[0] == 1 }
# returns [[2, 3], [3, 4]]
Run Code Online (Sandbox Code Playgroud)