# Dealing properly with scope; came from:combine array(string) to string?

**URL:** <https://www.ruby-forum.com/t/dealing-properly-with-scope-came-from-combine-array-string-to-string/132518>\
**Category:** Ruby\
**Created:** [March 21, 2008, 12:00am UTC](https://www.ruby-forum.com/t/dealing-properly-with-scope-came-from-combine-array-string-to-string/132518 "2008-03-21T00:00:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![toadkicker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5fc32e/32.png) [@toadkicker](https://www.ruby-forum.com/u/toadkicker)\
**Post date:** [March 21, 2008, 12:00am UTC](https://www.ruby-forum.com/t/dealing-properly-with-scope-came-from-combine-array-string-to-string/132518/1 "2008-03-21T00:00:22Z")

</div>

In much of my code, I’ve built an Array object before a block for  
scope reasons. Is this common practice?

b = %w|a b c d e|  
a = [] #instantiation  
b.each {|i| a \<\< i\*2}  
puts a

Todd

---

<div class="post-metadata">

**Author:** ![toadkicker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5fc32e/32.png) [@toadkicker](https://www.ruby-forum.com/u/toadkicker)\
**Post date:** [March 21, 2008, 12:16am UTC](https://www.ruby-forum.com/t/dealing-properly-with-scope-came-from-combine-array-string-to-string/132518/2 "2008-03-21T00:16:49Z")

</div>

You can usually use inject or collect to eliminate the a = [] line.  
For example:

b = %w[a b c d e]  
a = b.collect {|i| i \* 2 }

Also, I would caution against using the variable i as anything but a  
loop counter.

Regards,  
Dan

---

<div class="post-metadata">

**Author:** ![toadkicker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5fc32e/32.png) [@toadkicker](https://www.ruby-forum.com/u/toadkicker)\
**Post date:** [March 21, 2008, 8:15am UTC](https://www.ruby-forum.com/t/dealing-properly-with-scope-came-from-combine-array-string-to-string/132518/3 "2008-03-21T08:15:42Z")

</div>

On Thu, Mar 20, 2008 at 6:14 PM, Daniel F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> You can usually use inject or collect to eliminate the a = [] line.  
> For example:
> 
> b = %w[a b c d e]  
> a = b.collect {|i| i \* 2 }

Unless you have a method that doesn’t return an array (i.e. something  
other than map/collect). Sometimes you have to build the array inside  
the iterator. I was just wondering if this was common practice.

> Also, I would caution against using the variable i as anything but a  
> loop counter.

For production code, I agree. But, I think even then, I wouldn’t use  
“i”.

Todd

---

<div class="post-metadata">

**Author:** ![toadkicker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5fc32e/32.png) [@toadkicker](https://www.ruby-forum.com/u/toadkicker)\
**Post date:** [March 21, 2008, 9:56am UTC](https://www.ruby-forum.com/t/dealing-properly-with-scope-came-from-combine-array-string-to-string/132518/4 "2008-03-21T09:56:07Z")

</div>

On 21.03.2008 08:14, Todd B. wrote:

> On Thu, Mar 20, 2008 at 6:14 PM, Daniel F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > You can usually use inject or collect to eliminate the a = [] line.  
> > For example:
> > 
> > b = %w[a b c d e]  
> > a = b.collect {|i| i \* 2 }
> 
> Unless you have a method that doesn’t return an array (i.e. something  
> other than map/collect). Sometimes you have to build the array inside  
> the iterator. I was just wondering if this was common practice.

Yes, that’s perfectly ok. Often you can also use a variant using  
#inject, like

irb(main):003:0\> b = %w[a b c d e]  
=\> [“a”, “b”, “c”, “d”, “e”]  
irb(main):004:0\> b.inject([]){|ar,el| ar \<\< el\*2}  
=\> [“aa”, “bb”, “cc”, “dd”, “ee”]

But that would be silly in this case since there is #map / #collect.

Kind regards

robert
