Creating a array from the difference of two arrays

I want to make an array out of the difference of two different arrays

I have an array of users who own a page @page.users

I want the array of the users who do not own the page

Example:

@page.users = [kim, bob, sue]

and

@users = User.find(:all) = [kim, bob, sue, joe, jim]

then I want the difference between the two

@not_page_users = [joe, jim]

Thanks in advance -- K

Built in to the Array class. Array defines - to do exactly that

non_owners = @users - @page.users

(google ruby array difference)

Cheers Michael Johnston

@not_page_users = @users - @page.users thats all

Kim wrote:

I want to make an array out of the difference of two different arrays

I have an array of users who own a page @page.users

I want the array of the users who do not own the page

Example:

@page.users = [kim, bob, sue]

and

@users = User.find(:all) = [kim, bob, sue, joe, jim]

then I want the difference between the two

@not_page_users = [joe, jim]

Would you believe?:

    @not_page_users = @users - @page.users

Here's an example of where I think it would be really beneficial for those people coming to Rails without a lot of Ruby experience to either pick up a copy of the Pickaxe book or browse the contents of www.ruby-doc.org.

If you go to ruby-doc.org, click on "core library" and click on "Array", you will be amazed at how many awesome methods Ruby already has that make stuff like this so easy.

Hope I didn't come across as lecturing you (or anyone else). I just would hate to see Rails users who don't know much Ruby miss out on using the power of such an awesome programming language.

Jamey

Wow. I am feeling pretty stupid right now.

Jamey, You are right. I am new to Ruby and Rails and often forget that Ruby is a powerful language. I get confused on what is rails and what is ruby and where to look for what. I actually do visit the Rails API often but not the Ruby docs. I will now!

Thanks to everyone.

Hi --

Kim - I would order David's book :slight_smile: