Ruby on rails 如何构建长字符串?

Ruby on rails 如何构建长字符串?,ruby-on-rails,ruby,ruby-on-rails-3,string,Ruby On Rails,Ruby,Ruby On Rails 3,String,我正在使用RubyonRails3.2.2,我想用最简单的方法构建一个长字符串。我曾想过使用times方法,但使用以下代码并没有返回我要查找的代码: 10000.times{ "Foo bar" } # => 10000 我希望它返回“Foo bar Foo bar Foo bar Foo bar Foo bar Foo bar Foo bar Foo bar Foo bar Foo bar Foo bar Foo bar Foo bar Foo bar…” 我怎样才能做到 注意:我想在

我正在使用RubyonRails3.2.2,我想用最简单的方法构建一个长字符串。我曾想过使用
times
方法,但使用以下代码并没有返回我要查找的代码:

10000.times{ "Foo bar" }
# => 10000
我希望它返回
“Foo bar Foo bar Foo bar Foo bar Foo bar Foo bar Foo bar Foo bar Foo bar Foo bar Foo bar Foo bar Foo bar Foo bar…”

我怎样才能做到

注意:我想在my rspec文件中使用上述代码进行测试。

尝试以下方法:

"Long string"*1000

您所做的工作不起作用,因为您正在对整数1000调用
times
方法。它需要一个块,最后返回值1000

最简单的解决方案是对字符串调用乘法方法/运算符

就像@gmile建议的那样:

"Foo bar " * 10000
long_string = ''
10000.times{ |s| s << 'Foo bar ' }
puts long_string   # "Foo bar Foo bar Foo bar ..."
但是如果你真的想使用
10000.times{}
你可以这样做:

"Foo bar " * 10000
long_string = ''
10000.times{ |s| s << 'Foo bar ' }
puts long_string   # "Foo bar Foo bar Foo bar ..."
长字符串=“”

10000.times{124; s | s~gmile有最好的解决方案,但是比这个解决方案好的是10000.times.injection(“”){124; str,忽略| str