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.
Jeffrey B. (Guest)
on 2007-07-30 20: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
Dan Stevens (IAmAI) (Guest)
on 2007-07-30 20:22
(Received via mailing list)
@status_array << @status_number unless @status_array.include?
@status_number
Brett S. (Guest)
on 2007-07-30 21: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
Thomas W. (Guest)
on 2007-07-30 21:21
(Received via mailing list)
2007/7/30, Jeffrey B. <removed_email_address@domain.invalid>:
> 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_
Robert K. (Guest)
on 2007-07-31 01:05
(Received via mailing list)
On 30.07.2007 19:16, Brett S. 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.
Gordon T. (Guest)
on 2007-07-31 02:11
(Received via mailing list)
On Jul 30, 11:17 am, Jeffrey B. <removed_email_address@domain.invalid> 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.
Tim H. (Guest)
on 2007-07-31 02:24
(Received via mailing list)
Gordon T. 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.
Bertram S. (Guest)
on 2007-07-31 02:47
(Received via mailing list)
Hi,

Am Dienstag, 31. Jul 2007, 07:22:48 +0900 schrieb Tim H.:
> 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
Michael H. (Guest)
on 2007-07-31 03:26
(Received via mailing list)
Jeffrey B. 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
Michael H. (Guest)
on 2007-07-31 06:21
(Received via mailing list)
Michael H. 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.