# Arrays

**URL:** <https://www.ruby-forum.com/t/arrays/185725>\
**Category:** Ruby\
**Created:** [March 25, 2010, 5:27am UTC](https://www.ruby-forum.com/t/arrays/185725 "2010-03-25T05:27:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mailtorajuit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed8c4c/32.png) [@mailtorajuit](https://www.ruby-forum.com/u/mailtorajuit)\
**Post date:** [March 25, 2010, 5:27am UTC](https://www.ruby-forum.com/t/arrays/185725/1 "2010-03-25T05:27:05Z")

</div>

Hi all,

i have an array contains no of elements. i want to concatinate 91 before  
every elemnt of array. example

i have  
A= [234,456,raju] like this  
i want the out put arry tobe A=[91234,91456,91raju] like thissss

i want to add 91 before every element

how to do this

---

<div class="post-metadata">

**Author:** ![mailtorajuit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed8c4c/32.png) [@mailtorajuit](https://www.ruby-forum.com/u/mailtorajuit)\
**Post date:** [March 25, 2010, 5:38am UTC](https://www.ruby-forum.com/t/arrays/185725/2 "2010-03-25T05:38:52Z")

</div>

On Wed, Mar 24, 2010 at 10:27 PM, Rajkumar S.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> ## how to do this
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

## the assignment of the variable should look more like this:

## a = [“234”,“456”,“raju”]

because otherwise raju is a variable, which wouldn’t allow you to append  
91  
to the beginning of it.

## Try this then:

## array.collect! do |element| “91” + element.to\_s end

Mario

---

<div class="post-metadata">

**Author:** ![mailtorajuit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed8c4c/32.png) [@mailtorajuit](https://www.ruby-forum.com/u/mailtorajuit)\
**Post date:** [March 25, 2010, 6:36am UTC](https://www.ruby-forum.com/t/arrays/185725/3 "2010-03-25T06:36:55Z")

</div>

And the short form

> a = [“123”, “456”, “raju”]  
> a.map!{|x|“91#{x}”} # =\> [“91123”, “91456”, “91raju”]

---

<div class="post-metadata">

**Author:** ![mailtorajuit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed8c4c/32.png) [@mailtorajuit](https://www.ruby-forum.com/u/mailtorajuit)\
**Post date:** [March 25, 2010, 1:25pm UTC](https://www.ruby-forum.com/t/arrays/185725/4 "2010-03-25T13:25:57Z")

</div>

2010/3/25 Brandon J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> And the short form
> 
> > a = [“123”, “456”, “raju”]  
> > a.map!{|x|“91#{x}”} # =\> [“91123”, “91456”, “91raju”]

Even shorter (6 chars if I’m not mistaken):

a = %w{123 456 raju}  
a.map!{|x|“91#{x}”}

😉

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![mailtorajuit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed8c4c/32.png) [@mailtorajuit](https://www.ruby-forum.com/u/mailtorajuit)\
**Post date:** [March 25, 2010, 11:18pm UTC](https://www.ruby-forum.com/t/arrays/185725/5 "2010-03-25T23:18:28Z")

</div>

2010/3/25 Brandon J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > robert
> 
> oh yeah?!? well take this
> 
> %w(123 456 raju).map{|x|“91#{x}”}
> 
> not exactly the same, as there is no persistent variable, but it is a  
> bit shorter

You’re absolutely right. My main point - which I failed to mention -  
was, that arrays of strings can be easier represented with the %w  
notation. This works of course only if strings need not contain white  
space.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![mailtorajuit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed8c4c/32.png) [@mailtorajuit](https://www.ruby-forum.com/u/mailtorajuit)\
**Post date:** [March 25, 2010, 10:52pm UTC](https://www.ruby-forum.com/t/arrays/185725/6 "2010-03-25T22:52:51Z")

</div>

Robert K. wrote:

> Even shorter (6 chars if I’m not mistaken):
> 
> a = %w{123 456 raju}  
> a.map!{|x|“91#{x}”}
> 
> 😉
> 
> Kind regards
> 
> robert

oh yeah?!? well take this

%w(123 456 raju).map{|x|“91#{x}”}

not exactly the same, as there is no persistent variable, but it is a  
bit shorter

---

<div class="post-metadata">

**Author:** ![mailtorajuit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed8c4c/32.png) [@mailtorajuit](https://www.ruby-forum.com/u/mailtorajuit)\
**Post date:** [March 30, 2010, 4:30am UTC](https://www.ruby-forum.com/t/arrays/185725/7 "2010-03-30T04:30:17Z")

</div>

On Thu, Mar 25, 2010 at 11:17 PM, Robert K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> > >
> 
> was, that arrays of strings can be easier represented with the %w  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

I think %w[] would be more syntactically meaningful than curly  
braces, square brackets remind us of  
arrays. I think it is especially important when using shortcuts, which  
can  
be obscure, to be as clear as possible.
