[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Array help needed

user@domain.invalid

2/26/2008 2:18:00 PM

Hello, sorry if this question is stupid but I can't find a solution
after several readings about Arrays

Array 'a' contains several objets (ruby classes instances), each
instance have an id property


I need a clean solution to return true if array 'a' contains an instance
with id foo without iterating the whole array.

Example

def add_user(user)
unless users.include?(user.id)
users << user
end
end

users.include? statement is wrong because we compare id's with objects.
Which is the right way to do it ?


Thanks
8 Answers

Jano Svitok

2/26/2008 2:37:00 PM

0

On Tue, Feb 26, 2008 at 3:20 PM, Zouplaz <user@domain.invalid> wrote:
> Hello, sorry if this question is stupid but I can't find a solution
> after several readings about Arrays
>
> Array 'a' contains several objets (ruby classes instances), each
> instance have an id property
>
>
> I need a clean solution to return true if array 'a' contains an instance
> with id foo without iterating the whole array.
>
> Example
>
> def add_user(user)
- unless users.include?(user.id)
+ unless users.any? {|u| user.id == u.id }
> users << user
> end
> end

Craig Demyanovich

2/26/2008 2:41:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

On Tue, Feb 26, 2008 at 9:36 AM, Jano Svitok <jan.svitok@gmail.com> wrote:

> On Tue, Feb 26, 2008 at 3:20 PM, Zouplaz <user@domain.invalid> wrote:
> > Hello, sorry if this question is stupid but I can't find a solution
> > after several readings about Arrays
> >
> > Array 'a' contains several objets (ruby classes instances), each
> > instance have an id property
> >
> >
> > I need a clean solution to return true if array 'a' contains an
> instance
> > with id foo without iterating the whole array.
> >
> > Example
> >
> > def add_user(user)
> - unless users.include?(user.id)
> + unless users.any? {|u| user.id == u.id }
> > users << user
> > end
> > end


If you can't do this

def add_user(user)
unless users.include?(user) # just check the user instead of the id
users << user
end
end

then I'd also recommend Jano's solution.

Regards,
Craig

Robert Klemme

2/26/2008 2:46:00 PM

0

2008/2/26, Jano Svitok <jan.svitok@gmail.com>:
> On Tue, Feb 26, 2008 at 3:20 PM, Zouplaz <user@domain.invalid> wrote:
> > Hello, sorry if this question is stupid but I can't find a solution
> > after several readings about Arrays
> >
> > Array 'a' contains several objets (ruby classes instances), each
> > instance have an id property
> >
> >
> > I need a clean solution to return true if array 'a' contains an instance
> > with id foo without iterating the whole array.
> >
> > Example
> >
> > def add_user(user)
> - unless users.include?(user.id)
>
> + unless users.any? {|u| user.id == u.id }
> > users << user
> > end
> > end

For a more efficient solution use a Hash:

def add_user(user)
@users[user.id] ||= user
end

def users
@users.values
end

Kind regards

robert

--
use.inject do |as, often| as.you_can - without end

Christopher Swasey

2/26/2008 2:49:00 PM

0

On 2/26/08, Zouplaz <user@domain.invalid> wrote:
> Hello, sorry if this question is stupid but I can't find a solution
> after several readings about Arrays
>
> Array 'a' contains several objets (ruby classes instances), each
> instance have an id property
>
>
> I need a clean solution to return true if array 'a' contains an instance
> with id foo without iterating the whole array.
>
> Example
>
> def add_user(user)
> unless users.include?(user.id)
> users << user
> end
> end
>
> users.include? statement is wrong because we compare id's with objects.
> Which is the right way to do it ?

Read up on the Enumerable module.
http://www.ruby-doc.org/core/classes/Enumerable.ht....

You have a couple of options. The simplest code-wise is to use find:

def add_user(user)
unless users.find { |u| u.id == user.id }
users << user
end
end

Potentially marginally more efficient is #map and then calling
#include? on the result

def add_user(user)
unless users.map { |u| u.id }.include?(user.id)
users << user
end
end

Christopher

Christopher Swasey

2/26/2008 3:03:00 PM

0

On 2/26/08, Christopher Swasey <christopher.swasey@gmail.com> wrote:
> On 2/26/08, Zouplaz <user@domain.invalid> wrote:
> > Hello, sorry if this question is stupid but I can't find a solution
> > after several readings about Arrays
> >
> > Array 'a' contains several objets (ruby classes instances), each
> > instance have an id property
> >
> >
> > I need a clean solution to return true if array 'a' contains an instance
> > with id foo without iterating the whole array.
> >
> > Example
> >
> > def add_user(user)
> > unless users.include?(user.id)
> > users << user
> > end
> > end
> >
> > users.include? statement is wrong because we compare id's with objects.
> > Which is the right way to do it ?
>
>
> Read up on the Enumerable module.
> http://www.ruby-doc.org/core/classes/Enumerable.ht....
>
> You have a couple of options. The simplest code-wise is to use find:
>
> def add_user(user)
> unless users.find { |u| u.id == user.id }
> users << user
> end
> end
>
> Potentially marginally more efficient is #map and then calling
> #include? on the result
>
> def add_user(user)
> unless users.map { |u| u.id }.include?(user.id)
> users << user
> end
> end
>
>
> Christopher
>

Additionally, if a unique user always has the same id (for instance,
they're defined in a database and the id is the primary key), then you
can override #== in your class to compare ids to determine equality.
Then you can just call #include? with the user directly:

def add_user(user)
unless users.include?(user)
users << user
end
end

This is almost definitely the most efficient solution, but it does
require you to modify your class.

Best,
Christopher

user@domain.invalid

2/26/2008 10:28:00 PM

0

le 26/02/2008 15:36, Jano Svitok nous a dit:

>>
>> def add_user(user)
> - unless users.include?(user.id)
> + unless users.any? {|u| user.id == u.id }
>> users << user
>> end
>> end
>

Fine ! This is was I was looking for.

Thanks to people who answered...

BeamMeUpScotty

1/5/2013 6:13:00 PM

0

On 1/5/2013 12:57 PM, Nickname unavailable wrote:
> On Jan 5, 2:03 am, "Mr.B1ack" <nowh...@nada.net> wrote:
>> On Fri, 04 Jan 2013 09:19:37 -0800, Nickname unavailable wrote:
>>> as some southern states turn blue, unionization is exploding:)): Not
>>> since the Depression era has Florida experienced an explosion
>>
>> Well, theoretically, unionization is a GOOD thing ... a way
>> for the poorer working class to weild as much power as the
>> monied upper class.
>>
>> Theoretically ...
>
> nothing is perfect.
>
Theoretically a PIG could fly with enough wing area for lift.... but
where would they want to fly, and what's the benefit of having flying pigs?




Nickname unavailable

1/6/2013 8:24:00 AM

0

On Jan 6, 1:33 am, "Mr.B1ack" <nowh...@nada.net> wrote:
> On Sat, 05 Jan 2013 09:57:55 -0800, Nickname unavailable wrote:
> > On Jan 5, 2:03 am, "Mr.B1ack" <nowh...@nada.net> wrote:
> >> On Fri, 04 Jan 2013 09:19:37 -0800, Nickname unavailable wrote:
> >> > as some southern states turn blue, unionization is exploding:)): Not
> >> > since the Depression era has Florida experienced an explosion
>
> >>    Well, theoretically, unionization is a GOOD thing ... a way for
> >>    the poorer working class to weild as much power as the monied
> >>    upper class.
>
> >>    Theoretically ...
>
> >  nothing is perfect.
>
>    No.
>
>    However there are degrees of imperfection.
>
>    Many unions rank rather low in that respect.

they rank far higher than "THE CONSERVATIVES" in congress.