来自另一个在Ruby中减去1阵列 [英] Subtracting one Array from another in Ruby

查看:111
本文介绍了来自另一个在Ruby中减去1阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有任务的两个数组 - 创建和分配。
我想从创建的任务数组中删除所有分配任务。
这是我的工作,但凌乱,code:

I've got two arrays of Tasks - created and assigned. I want to remove all assigned tasks from the array of created tasks. Here's my working, but messy, code:

	@assigned_tasks = @user.assigned_tasks
	@created_tasks = @user.created_tasks

	#Do not show created tasks assigned to self
	@created_not_doing_tasks = Array.new
	@created_tasks.each do |task|
		unless @assigned_tasks.include?(task)
			@created_not_doing_tasks << task
		end
	end

我敢肯定有一个更好的办法。它是什么?
感谢: - )

I'm sure there's a better way. What is it? Thanks :-)

推荐答案

您可以减去阵列在Ruby中:

You can subtract arrays in Ruby:

[1,2,3,4,5] - [1,3,4]  #=> [2,5]

查看阵列文档。

这篇关于来自另一个在Ruby中减去1阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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