如何在 Ruby 中创建字符串和整数序列?

How to create a String and Integer sequence in Ruby?

瞄准

目的是在 Ruby 中创建一个字符串和整数序列,即 hello0, hello1, hello2hello 表示字符串,数字:1,2 and 3 是整数。

尝试

seq=(0..3).to_a

returns

0123

问题

如何在 Ruby 中创建字符串和整数序列?

(0..3).to_a.map {|e| "hello#{e}" }

The aim is to create a text and integer sequence in Ruby, i.e., hello0, hello1, hello2

您可以将区块传递给 Array::new:

Array.new(3) { |i| "hello#{i}" }
#=> ["hello0", "hello1", "hello2"]