2015-02-23 17 views
-1

目的如何在Ruby中創建一個字符串和整數序列?

的目的是創造一個字符串並且在紅寶石整數序列,即,hello0, hello1, hello2hello代表一個字符串,數字:1,2 and 3是整數。

嘗試

seq=(0..3).to_a

回報

0123

問題

如何在Ruby中創建一個字符串和整數序列?

+1

喜歡這個? '[ 「你好」]的產物((0..3).to_a).MAP(&:加入)。#=> [ 「hello0」, 「hello1」, 「hello2」, 「hello3」]' – engineersmnky 2015-02-23 16:20:13

+0

@engineersmnky作品。請添加它作爲答案。 – 030 2015-02-23 16:25:10

+0

你的問題是什麼? – sawa 2015-02-23 16:36:02

回答

3

目的是在Ruby來創建文本和整數序列,即,hello0, hello1, hello2

可以一個塊傳遞到Array::new

Array.new(3) { |i| "hello#{i}" } 
#=> ["hello0", "hello1", "hello2"] 
1
(0..3).to_a.map {|e| "hello#{e}" } 
相關問題