views:

589

answers:

1

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 :-)

+6  A: 

You can subtract arrays in Ruby:

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

See the Array documentation.

hobodave
Arg. Big face-palm moment for me. For some reason I thought that wouldn't work with objects. Worked just fine - thanks!
doctororange
np, it happens :)
hobodave