Reputation: 4792
I am using Ruby on Rails 3.2.2 and I would like to build a long string the easiest way. I thought of use the times
method, but using the following code it doesn't return that I am looking for:
10000.times{ "Foo bar" }
# => 10000
I would like that it returns "Foo bar Foo bar Foo bar Foo bar Foo bar Foo bar Foo bar Foo bar Foo bar Foo bar ..."
.
How can I make that?
Note: I would like to use the above code for testing purposes in a my rspec file.
Upvotes: 0
Views: 908
Reputation: 12273
What you're doing doesn't work because you're calling the times
method on the integer 1000. It's taking a block and ends up returning the value 1000.
The easiest solution is to call the multiplication method/operator on a string.
So like @gmile suggested do it like this:
"Foo bar " * 10000
But if you really want to use 10000.times{ }
you can do so like this:
long_string = ''
10000.times{ |s| s << 'Foo bar ' }
puts long_string # "Foo bar Foo bar Foo bar ..."
Upvotes: 0