Ruby: prints something x times

I want to return string 'x' a number of times. My current code

return str + str + str + str

      

What should I do to do something like

return 4 * str

      

+3


source to share


3 answers


You are close ...



return str * 4

      

+4


source


You can try this



4.times{puts string}

      

0


source


String * integer gives you a new string containing whole copies of the recipient, and the integer must be equal to or greater than 0. For example:> "Hello World!" * 3 ==> "Hello World! Hello World! Hello World!"

Link http://ruby-doc.org/core-2.3.1/String.html

0


source







All Articles