# Range question

**URL:** <https://www.ruby-forum.com/t/range-question/79941>\
**Category:** Ruby\
**Created:** [December 20, 2006, 6:42pm UTC](https://www.ruby-forum.com/t/range-question/79941 "2006-12-20T18:42:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [December 20, 2006, 6:42pm UTC](https://www.ruby-forum.com/t/range-question/79941/1 "2006-12-20T18:42:00Z")

</div>

Hi all,

I want to create an array as following:  
a4=[‘name 1’,‘name 2’…‘name 20’].

It looks like a range for me. But I can’t find a  
method in class Range to create this array. Here is my  
script I am not sure if this the Ruby way to do this.  
Any inputs?

Li

# 

str=‘name 1’  
a4=[]  
1.upto(20) do|i|  
a4 \<\<(str.split()[0]+" #{i}")  
end

p a4  
#output  
[“name 1”, “name 2”, “name 3”, “name 4”, “name 5”,  
“name 6”, “name 7”, “name 8”, “name 9”, “name 10”,  
“name 11”, “name 12”, “name 13”, “name 14”, “name 15”,  
“name 16”, “name 17”, “name 18”, “name 19”, “name 20”]

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [December 20, 2006, 6:44pm UTC](https://www.ruby-forum.com/t/range-question/79941/2 "2006-12-20T18:44:00Z")

</div>

chen li a écrit :

> Li  
> [“name 1”, “name 2”, “name 3”, “name 4”, “name 5”,  
> “name 6”, “name 7”, “name 8”, “name 9”, “name 10”,  
> “name 11”, “name 12”, “name 13”, “name 14”, “name 15”,  
> “name 16”, “name 17”, “name 18”, “name 19”, “name 20”]
> 
> > Exit code: 0

I will do it like this :

$ irb

> > (1…20).map { |i| “name #{i}” }  
> > =\> [“name 1”, “name 2”, “name 3”, “name 4”, “name 5”, “name 6”, “name  
> > 7”, “name 8”, “name 9”, “name 10”, “name 11”, “name 12”, “name 13”,  
> > “name 14”, “name 15”, “name 16”, “name 17”, “name 18”, “name 19”, “name  
> > 20”]

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [December 20, 2006, 7:53pm UTC](https://www.ruby-forum.com/t/range-question/79941/3 "2006-12-20T19:53:15Z")

</div>

On Thu, 21 Dec 2006, chen li wrote:

> Hi all,
> 
> I want to create an array as following:  
> a4=[‘name 1’,‘name 2’…‘name 20’].

a4 = Array.new(20){|i| “name #{ i + 1 }”}

-a

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [December 20, 2006, 6:52pm UTC](https://www.ruby-forum.com/t/range-question/79941/4 "2006-12-20T18:52:05Z")

</div>

> It looks like a range for me. But I can’t find a  
> method in class Range to create this array. Here is my  
> script I am not sure if this the Ruby way to do this.  
> Any inputs?

(“blah 01”…“blah 20”).each {|e| puts e}

hope that helps,

```
                    UG

```

* * *

Uma G.

> **[The Umasphere](https://umageller.wordpress.com)**
>
> Uma Geller dreams by day and types by night

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [December 20, 2006, 9:25pm UTC](https://www.ruby-forum.com/t/range-question/79941/5 "2006-12-20T21:25:56Z")

</div>

chen li wrote:

> Hi all,
> 
> I want to create an array as following:  
> a4=[‘name 1’,‘name 2’…‘name 20’].
> 
> It looks like a range for me. But I can’t find a  
> method in class Range to create this array. Here is my  
> script I am not sure if this the Ruby way to do this.  
> Any inputs?

(‘1’…‘20’).map{|s| 'name ’ + s}

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [December 20, 2006, 9:25pm UTC](https://www.ruby-forum.com/t/range-question/79941/6 "2006-12-20T21:25:55Z")

</div>

base = “name 00”  
ary = []  
20.times{ary \<\< base.succ!.dup}

or:

base = "name "  
ary = []  
1.upto(20) {|x| ary \<\< base + x.to\_s}

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [December 21, 2006, 9:28pm UTC](https://www.ruby-forum.com/t/range-question/79941/7 "2006-12-21T21:28:48Z")

</div>

Bob S. wrote:

> Range has a to\_a method, but it won’t work for ‘Name 1’…‘Name 20’ (it  
> does for ‘Name 01’…‘Name 20’ as pointed out).
> 
> I would use
> 
> arr = (1…20).collect{|i| “name #{i}”}

Thank all for your kindly inputs.

li

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [December 20, 2006, 9:37pm UTC](https://www.ruby-forum.com/t/range-question/79941/8 "2006-12-20T21:37:58Z")

</div>

On 12/20/06, chen li [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi all,
> 
> I want to create an array as following:  
> a4=[‘name 1’,‘name 2’…‘name 20’].
> 
> It looks like a range for me. But I can’t find a  
> method in class Range to create this array. Here is my  
> script I am not sure if this the Ruby way to do this.  
> Any inputs?

Range has a to\_a method, but it won’t work for ‘Name 1’…‘Name 20’ (it  
does for ‘Name 01’…‘Name 20’ as pointed out).

I would use

arr = (1…20).collect{|i| “name #{i}”}
