Range question

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"]

Exit code: 0

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around

chen li a écrit :

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"]

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"]

···

--
Bruno Michel

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 Geller

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

-a

···

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'].

--
if you find yourself slandering anybody, first imagine that your mouth is
filled with excrement. it will break you of the habit quickly enough. - the
dalai lama

chen li wrote:

Hi all,

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

a4 = (1..20).collect{|n| "name #{n}"}

Cheers,
Antonio

···

--

Zen and the Art of Ruby Programming

base = "name 00"
ary =
20.times{ary << base.succ!.dup}

or:

base = "name "
ary =
1.upto(20) {|x| ary << base + x.to_s}

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?

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"]

Exit code: 0

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com

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}

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}"}

···

On 12/20/06, chen li <chen_li3@yahoo.com> 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?

Bob Showalter 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

···

--
Posted via http://www.ruby-forum.com/\.