# Adding to an array question

**URL:** <https://www.ruby-forum.com/t/adding-to-an-array-question/112435>\
**Category:** Ruby\
**Created:** [August 18, 2007, 9:42pm UTC](https://www.ruby-forum.com/t/adding-to-an-array-question/112435 "2007-08-18T21:42:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![visage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/90db22/32.png) [@visage](https://www.ruby-forum.com/u/visage)\
**Post date:** [August 18, 2007, 9:42pm UTC](https://www.ruby-forum.com/t/adding-to-an-array-question/112435/1 "2007-08-18T21:42:57Z")

</div>

I need to add a string to the end of an array count number of times. I  
was wondering if I could get some advice on the prefered way to do it  
in Ruby.

@bucket is the array

def add\_person(count, name)  
@bucket += Array.new(count, name)  
end

or some other ideas

def add\_person(count, name)  
count.times {@bucket \<\< name }  
end

# or some variation of looping

Personally, I like the adding a new array to the @bucket array. I  
realize (at least I assume) that it creates a temporary Array, but I  
think it is more understandable?

Thoughts?  
Thanks, I’m just learning Ruby  
-Corey

---

<div class="post-metadata">

**Author:** ![visage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/90db22/32.png) [@visage](https://www.ruby-forum.com/u/visage)\
**Post date:** [August 19, 2007, 3:34am UTC](https://www.ruby-forum.com/t/adding-to-an-array-question/112435/2 "2007-08-19T03:34:41Z")

</div>

Corey H. wrote:

> I need to add a string to the end of an array count number of times. I  
> was wondering if I could get some advice on the prefered way to do it  
> in Ruby.
> 
> @bucket is the array
> 
> def add\_person(count, name)  
> @bucket += Array.new(count, name)  
> end
> 
> or some other ideas
> 
> def add\_person(count, name)  
> count.times {@bucket \<\< name }  
> end
> 
> # or some variation of looping
> 
> Personally, I like the adding a new array to the @bucket array. I  
> realize (at least I assume) that it creates a temporary Array, but I  
> think it is more understandable?
> 
> Thoughts?  
> Thanks, I’m just learning Ruby  
> -Corey

def add\_person(name, count=1) # I assume adding once as a default is  
rather safe  
@bucket.concat(Array.new(count) {Â name.dup }) # a) this will create a  
new string object everytime  
@bucket.concat(Array.new(count, name) # b) this will use the _same_  
string object everytime  
end

Use either a) or b). Be aware that with b) if you do e.g.  
@bucket.last.upcase!, it will reflect on all slots that contain the same  
object.  
As to why I prefer this concat solution over array +=: it doesn’t create  
throw away objects everytime.

Regards  
Stefan

---

<div class="post-metadata">

**Author:** ![visage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/90db22/32.png) [@visage](https://www.ruby-forum.com/u/visage)\
**Post date:** [August 19, 2007, 4:39pm UTC](https://www.ruby-forum.com/t/adding-to-an-array-question/112435/3 "2007-08-19T16:39:31Z")

</div>

Thanks for the tips, Stefan. I like your tip on doing .dup, as well.

Thanks again!

-Corey

---

<div class="post-metadata">

**Author:** ![visage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/90db22/32.png) [@visage](https://www.ruby-forum.com/u/visage)\
**Post date:** [August 20, 2007, 3:56am UTC](https://www.ruby-forum.com/t/adding-to-an-array-question/112435/4 "2007-08-20T03:56:29Z")

</div>

2 cents:

count.times {@bucket \<\< name }

Looks more readable to me.  
Mario
