# Missed elements in array

**URL:** <https://www.ruby-forum.com/t/missed-elements-in-array/180803>\
**Category:** Rails\
**Created:** [December 29, 2009, 3:23pm UTC](https://www.ruby-forum.com/t/missed-elements-in-array/180803 "2009-12-29T15:23:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![LeonS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c77e96/32.png) [@LeonS](https://www.ruby-forum.com/u/LeonS)\
**Post date:** [December 29, 2009, 3:23pm UTC](https://www.ruby-forum.com/t/missed-elements-in-array/180803/1 "2009-12-29T15:23:14Z")

</div>

Hi,  
i despair in case of an array.  
I’ll take some data into an array, but when i iterate the array some  
elements of the array seems to be missed, but the missing element are  
displayed in the view.  
I’m using the Plugin Rails\_Authorization.

my Code:

def index  
if !params[:task\_id] #Tasks der 1. ebene anzeigen, auf die der  
User zugriff hat  
@tasks = current\_user.is\_owner\_of\_what +  
current\_user.is\_moderator\_of\_what + current\_user.is\_user\_of\_what  
@tasks.uniq! #doppelte Einträge auf einen reduzieren  
puts @tasks  
@tasks.each do |task|  
puts task.id  
if !task.root? #ist Task kein wurzelelement?  
if (current\_user.has\_roles\_for? Task.find(task.parent\_id) )  
#Hat der Benutzer Rechte auf Parent Task?  
@tasks.delete(task) #Objekt aus Array schmeißen da nur  
die Elemente der 1. Ebene dargestellt werden sollen, auf die der  
Benutzer Rechte hat

```
      end
    end
  end
else #Wenn Subtasks angezeigt werden sollen
  @task=Task.find(params[:task_id])
  load_subtasks
  @tasks=@subtasks
end
#@tasks = Task.all
respond_to do |format|

  format.html #index.html.erb
  format.xml { render :xml => @tasks }
end

```

end

And My Debugging Log:

#Task:0x1036e6ca8  
#Task:0x1036e3ee0  
#Task:0x1036df958  
#Task:0x1036dc190  
#Task:0x1036d8ea0  
#Task:0x1036d5138  
#Task:0x1036d18f8  
#Task:0x1036cbd90  
5  
6  
8  
12  
14

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![LeonS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c77e96/32.png) [@LeonS](https://www.ruby-forum.com/u/LeonS)\
**Post date:** [December 29, 2009, 4:13pm UTC](https://www.ruby-forum.com/t/missed-elements-in-array/180803/2 "2009-12-29T16:13:20Z")

</div>

On Dec 29, 2:21 pm, LeonS [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,  
> i despair in case of an array.  
> I’ll take some data into an array, but when i iterate the array some  
> elements of the array seems to be missed, but the missing element are  
> displayed in the view.  
> I’m using the Plugin Rails\_Authorization.

You’re deleting from the array as you are iterating over it - don’t do  
that.

Fred

> ```
> puts task.id
> else #Wenn Subtasks angezeigt werden sollen
> 
> ```
> 
> end  
> #Task:0x1036cbd90  
> 5  
> 6  
> 8  
> 12  
> 14

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![LeonS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c77e96/32.png) [@LeonS](https://www.ruby-forum.com/u/LeonS)\
**Post date:** [December 29, 2009, 6:33pm UTC](https://www.ruby-forum.com/t/missed-elements-in-array/180803/3 "2009-12-29T18:33:42Z")

</div>

Frederick C. wrote:

> On Dec 29, 2:21ï¿½pm, LeonS [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Hi,  
> > i despair in case of an array.  
> > I’ll take some data into an array, but when i iterate the array some  
> > elements of the array seems to be missed, but the missing element are  
> > displayed in the view.  
> > I’m using the Plugin Rails\_Authorization.
> 
> You’re deleting from the array as you are iterating over it - don’t do  
> that.

Yep. A common mistake when iterating arrays. One solution is to build a  
new array containing the objects you want, rather than deleting objects  
that you don’t want. There may be better approaches that might use less  
memory though.

---

<div class="post-metadata">

**Author:** ![LeonS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c77e96/32.png) [@LeonS](https://www.ruby-forum.com/u/LeonS)\
**Post date:** [December 29, 2009, 7:48pm UTC](https://www.ruby-forum.com/t/missed-elements-in-array/180803/4 "2009-12-29T19:48:13Z")

</div>

On Dec 29, 5:33 pm, Robert W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Yep. A common mistake when iterating arrays. One solution is to build a  
> new array containing the objects you want, rather than deleting objects  
> that you don’t want. There may be better approaches that might use less  
> memory though.

And if you do want to do that, a rubyish way of doing that is to use  
methods like select

The relevant portion of the code above could be written as

@tasks = @tasks.select {|task| task.root? || !  
current\_user.has\_roles\_for?(Task.find(task.parent\_id))}

(probably a little faster too - Array#delete is O(n))

Fred

> –  
> Posted viahttp://www.ruby-forum.com/.

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).
