Array.include?

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

@status_array << @status_number unless @status_array.include?
@status_number

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

2007/7/30, Jeffrey B. [email protected]:

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

On Jul 30, 11:17 am, Jeffrey B. [email protected] 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.

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. 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.

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. 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.

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