Forum: Ruby array.include?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7070c0ca59124b4d96dd27ea52ad359e?d=identicon&s=25 Jeffrey Bowen (Guest)
on 2007-07-30 18:19
(Received via mailing list)
if @status_array.include? @status_number
else
   @status_array << @status_number
end

I'm using the above to append an array if it does not
include @status_number.  Is there an include?-not
function that would eliminate the else?  Or another
way to do the same thing?

Jeff
2b4da3f15e2d0f58be623bf40795de07?d=identicon&s=25 Dan Stevens (IAmAI) (Guest)
on 2007-07-30 18:22
(Received via mailing list)
@status_array << @status_number unless @status_array.include?
@status_number
D046d3c8b40b45f19044671cf6cf40f9?d=identicon&s=25 Brett Simmers (Guest)
on 2007-07-30 19:18
(Received via mailing list)
Do you need the order of the items to be preserved?  If not, you should
think about using a Set instead of an Array.  You could then use
Set#add, which accomplishes the same thing and is MUCH faster,
especially if your data set gets fairly large.  Array#include? has to do
a linear search over the entire Array, but most operations on Sets are
constant time, including Set#add, Set#add?, and Set#include?

Brett
88de1beaaacae26b839f2e6aebf857b6?d=identicon&s=25 Thomas Wieczorek (Guest)
on 2007-07-30 19:21
(Received via mailing list)
2007/7/30, Jeffrey Bowen <ja_bowen@yahoo.com>:
> if @status_array.include? @status_number
> else
>    @status_array << @status_number
> end
>
> Is there an include?-not > function that would eliminate the else?
>
You can use _not_ in the if statement:
if not @status_array.include? @status_number
  @status_array << @status_number
end

or

@status_array << @status_number if not @status_array.include?
@status_number

or like Dan did it with _unless_
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2007-07-30 23:05
(Received via mailing list)
On 30.07.2007 19:16, Brett Simmers wrote:
> Do you need the order of the items to be preserved?  If not, you should
> think about using a Set instead of an Array.  You could then use
> Set#add, which accomplishes the same thing and is MUCH faster,
> especially if your data set gets fairly large.  Array#include? has to do
> a linear search over the entire Array, but most operations on Sets are
> constant time, including Set#add, Set#add?, and Set#include?

I'd also say that a Set seems most appropriate here (a Hash might as
well, depending on what has to be done with the data).  And you can even
use << with a Set.

Kind regards

  robert


PS: Please do not top post.
Ec07fbda510a142f5331ffd903b07820?d=identicon&s=25 Gordon Thiesfeld (Guest)
on 2007-07-31 00:11
(Received via mailing list)
On Jul 30, 11:17 am, Jeffrey Bowen <ja_bo...@yahoo.com> wrote:
> if @status_array.include? @status_number
> else
>    @status_array << @status_number
> end

How about not checking at all?  Just put them all in there and then
use @status_array.uniq! when necessary?  I'm not advocating this.
Just curious what people think.
3afd3e5e05dc9310c89aa5762cc8dd1d?d=identicon&s=25 Tim Hunter (Guest)
on 2007-07-31 00:24
(Received via mailing list)
Gordon Thiesfeld wrote:
> Just curious what people think.
>
>
>
If it was me, I'd just use a hash instead of an array, with
@status_number as the key and anything (1, for example) as the value.
Let the hash object figure out if @status_number is already a key. It'll
be fast, and any time you need a list of status_numbers, just call
hash.keys.
C06869c119472a139eb163b72040b0db?d=identicon&s=25 Bertram Scharpf (Guest)
on 2007-07-31 00:47
(Received via mailing list)
Hi,

Am Dienstag, 31. Jul 2007, 07:22:48 +0900 schrieb Tim Hunter:
> Let the hash object figure out if @status_number is already a key. It'll
> be fast, and any time you need a list of status_numbers, just call
> hash.keys.

Probably it is useful to count them.

  @status_hash = Hash.new 0
  ...
  @status_hash[ status_number] += 1

Bertram
83668557add122451315d38b24a9fe62?d=identicon&s=25 Michael Hollins (Guest)
on 2007-07-31 01:26
(Received via mailing list)
Jeffrey Bowen wrote:
> if @status_array.include? @status_number
> else
>    @status_array << @status_number
> end
>
> I'm using the above to append an array if it does not
> include @status_number.  Is there an include?-not
> function that would eliminate the else?  Or another
> way to do the same thing?

The simplest rewrite of your code is:

if !@status_array.include? @status_number
    @status_array << @status_number
end
83668557add122451315d38b24a9fe62?d=identicon&s=25 Michael Hollins (Guest)
on 2007-07-31 04:21
(Received via mailing list)
Michael Hollins wrote:
>
> The simplest rewrite of your code is:
>
> if !@status_array.include? @status_number
>    @status_array << @status_number
> end

Apologies for duplicating other replies. For some reason I didn't notice
them before sending my reply.
This topic is locked and can not be replied to.