You are on page 1of 18

12/6/2016

LaunchSchoolanonlineschoolfordevelopers

Entercomment

Selectacategory Yourcomment
Edit Sendfeedback
Preview Sendfeedback

RepeatYourself
Writeamethodthattakestwoarguments,astringandapositiveinteger,andprintsthestringasmanytimesas
theintegerindicates.
Example:
repeat('Hello',3)

Output:
Hello
Hello
Hello

HideSolution&Discussion

Solution
defrepeat(string,number)
number.timesdo
putsstring
end
end

Discussion
Whensolvingexercises,itcanbebeneficialtoprogressinsmallincrements.Westartedoutbydefiningrepeat
withtwoparameters.Then,toensureeverythingworkedproperly,weaddedputsstringinsidethemethodand
ranthecode.
defrepeat(string,number)
putsstring
end

https://launchschool.com/exercises/a018e581

2/19

12/6/2016

LaunchSchoolanonlineschoolfordevelopers

Thisworkscorrectly,however,itonlyprintsstringonce.Ourgoalistoprintstringaspecifiednumberof
times.OneofthemoresuitablemethodsforthissituationisInteger#times.Usingthismethod,wecanexecute
putsstringanynumberoftimes.Inthiscase,wewanttoprintstringthenumberoftimesindicatedbynumber.
Watch

YoumarkedthisexerciseascompletedGotothenextexercise
Addyouruniquesolutionbelow
Wewanttoinviteyoutocontributeyouruniquesolutionoranalysis.Thegoalhereistoshowcasethevarietyofwaysinwhichthis
exercisecanbesolved,andthevarioustradeoffsonehastoconsiderwhenworkingtoasolution.Pleasedonotsubmitredundant
solutions,orsolutionsyouhaven'ttested,orsolutionsthatcontainerrors.It'snotmandatorythatyousubmitasolution.Ifyoudo,
makesureit'sasomewhatgoodsolutionthatmaybeusefultootherswhoarelearning.
Usetheformbelowtoaddyoursolutionoranalysisforthisexercise.Youcanincludealinktoagistorpastebin,orwriteyourentire
solutionrighthere.Makesuretouseproperformattingsoyourcodeshowsupcorrectlyandpreviewyourcodefirst.Youmayinclude
somecommentaryaboutyourcodeandthetradeoffsyoutook,orsomeanalysisaboutthisexerciseanditssolutions.Makesureyour
solutionfulfillsalloftheprovidedexamples,ifany,anddoesn'tcontainanyobviouserrors.
Don'tusetheformbelowtoaskquestionsormakesuggestionsabouttheexercise.Ifyouhaveaquestion,askinourCommunity
Forums.Ifyoufoundanerrororhaveasuggestion,usetheFeedbackformontherightsideofthispage.
Addyoursolutionor
analysis

NotifymeoffuturesubmissionsFormattingHelpPreview Submit

Edit Submit
Showusersubmittedsolutions

ColtonEhrman
adayago
defrepeat(string,num)
puts((string+'')*num).split
end
repeat('Hello',3)

EditSubmission
```ruby
defrepeat(string,
num)

Notifyuserswhoarewatchingthisexercise

Keepothersupdatedbycheckingthisboxwhenmakingsubstantialchanges.Don'tcheckwhenmakingsmallgrammaticalfixes.

FormattingHelpPreview Update Cancel


Edit Update
0Likes

Comment
CreateComment
Commentonthis
solution

FormattingHelpPreview Post Cancel

Edit Post

RichardAhn
https://launchschool.com/exercises/a018e581

3/19

12/6/2016

LaunchSchoolanonlineschoolfordevelopers

5daysago
defrepeat(string,number)number.timesdoputs'hello'endend
EditSubmission
defrepeat(string,
number)
number.timesdo

Notifyuserswhoarewatchingthisexercise

Keepothersupdatedbycheckingthisboxwhenmakingsubstantialchanges.Don'tcheckwhenmakingsmallgrammaticalfixes.

FormattingHelpPreview Update Cancel


Edit Update
0Likes

Comment
CreateComment
Commentonthis
solution

FormattingHelpPreview Post Cancel

Edit Post

RobDonigian
6daysago
defrepeat(some_str,some_int)
some_int>0?some_int.times{putssome_str}:"Pleaseenterapositiveinteger."
end

EditSubmission
```
def
repeat(some_str,

Notifyuserswhoarewatchingthisexercise

Keepothersupdatedbycheckingthisboxwhenmakingsubstantialchanges.Don'tcheckwhenmakingsmallgrammaticalfixes.

FormattingHelpPreview Update Cancel


Edit Update
0Likes

Comment
CreateComment
Commentonthis
solution

FormattingHelpPreview Post Cancel

Edit Post

zane
13daysago
repeat(sting,num)
num.timesdo|string|
putsstring
end
end

EditSubmission
https://launchschool.com/exercises/a018e581

4/19

12/6/2016

```ruby
repeat(sting,num)
num.timesdo|

LaunchSchoolanonlineschoolfordevelopers

Notifyuserswhoarewatchingthisexercise

Keepothersupdatedbycheckingthisboxwhenmakingsubstantialchanges.Don'tcheckwhenmakingsmallgrammaticalfixes.

FormattingHelpPreview Update Cancel


Edit Update
0Likes

Comment
CreateComment
Commentonthis
solution

FormattingHelpPreview Post Cancel

Edit Post

AndrewDutton
27daysago
defrepeat(str,num)
puts"#{str}\n"*num
end

EditSubmission
```ruby
defrepeat(str,
num)

Notifyuserswhoarewatchingthisexercise

Keepothersupdatedbycheckingthisboxwhenmakingsubstantialchanges.Don'tcheckwhenmakingsmallgrammaticalfixes.

FormattingHelpPreview Update Cancel


Edit Update
0Likes

Comment
CreateComment
Commentonthis
solution

FormattingHelpPreview Post Cancel

Edit Post

MarkDames
aboutamonthago

using.timesononelinewithbrackets
defrepeat(string,integer)
integer.times{putsstring}
end
repeat('Mark',10)

EditSubmission
#using.timeson
onelinewith
brackets

Notifyuserswhoarewatchingthisexercise

https://launchschool.com/exercises/a018e581

5/19

12/6/2016

LaunchSchoolanonlineschoolfordevelopers

Keepothersupdatedbycheckingthisboxwhenmakingsubstantialchanges.Don'tcheckwhenmakingsmallgrammaticalfixes.

FormattingHelpPreview Update Cancel


Edit Update
0Likes

Comment
CreateComment
Commentonthis
solution

FormattingHelpPreview Post Cancel

Edit Post

xiaoxiao
2monthsago
defrepeat(str,count)
count.times{putsstr}
end
repeat('Hello',3)

EditSubmission
```ruby
defrepeat(str

Notifyuserswhoarewatchingthisexercise

Keepothersupdatedbycheckingthisboxwhenmakingsubstantialchanges.Don'tcheckwhenmakingsmallgrammaticalfixes.

FormattingHelpPreview Update Cancel


Edit Update
0Likes

Comment
CreateComment
Commentonthis
solution

FormattingHelpPreview Post Cancel

Edit Post

JulieHonour
2monthsago
defrepeat(string,integer)
puts(string+'')*integer
end

EditSubmission
```ruby
defrepeat(string,
integer)

Notifyuserswhoarewatchingthisexercise

Keepothersupdatedbycheckingthisboxwhenmakingsubstantialchanges.Don'tcheckwhenmakingsmallgrammaticalfixes.

FormattingHelpPreview Update Cancel


Edit Update
https://launchschool.com/exercises/a018e581

6/19

12/6/2016

LaunchSchoolanonlineschoolfordevelopers

0Likes

Comment
CreateComment
Commentonthis
solution

FormattingHelpPreview Post Cancel

Edit Post

KaFaiChoi
2monthsago
defrepeat(str,int)int.timesdoputsstrendend
EditSubmission
defrepeat(str,int)
int.timesdo
putsstr

Notifyuserswhoarewatchingthisexercise

Keepothersupdatedbycheckingthisboxwhenmakingsubstantialchanges.Don'tcheckwhenmakingsmallgrammaticalfixes.

FormattingHelpPreview Update Cancel


Edit Update
0Likes

Comment
CreateComment
Commentonthis
solution

FormattingHelpPreview Post Cancel

Edit Post

AlexandraProdan
2monthsago
defrepeat(str,num)
puts(str+"\n")*num
end

EditSubmission
```ruby
defrepeat(str,
num)

Notifyuserswhoarewatchingthisexercise

Keepothersupdatedbycheckingthisboxwhenmakingsubstantialchanges.Don'tcheckwhenmakingsmallgrammaticalfixes.

FormattingHelpPreview Update Cancel


Edit Update
0Likes

Comment
CreateComment
Commentonthis
solution

FormattingHelpPreview Post Cancel

Edit Post

https://launchschool.com/exercises/a018e581

7/19

12/6/2016

LaunchSchoolanonlineschoolfordevelopers

CadellSingh
2monthsago
defrepeat(str,num)
num.times{putsstr}
end
repeat('Hello',3)

EditSubmission
```ruby
defrepeat(str,num)
num.times{puts

Notifyuserswhoarewatchingthisexercise

Keepothersupdatedbycheckingthisboxwhenmakingsubstantialchanges.Don'tcheckwhenmakingsmallgrammaticalfixes.

FormattingHelpPreview Update Cancel


Edit Update
1Like

Comment
CreateComment
Commentonthis
solution

FormattingHelpPreview Post Cancel

Edit Post

DavidGeorge
2monthsago

Twoways(ofanumber)torunastatementiteratively
defrepeater(string,count)
foreach_timein1.uptocount
putsstring
end
end
repeater'Hello',3
defrepeat(s,c)
c.times{|ix|putss}
end
repeat'doink',5

EditSubmission
#Twoways(ofa
number)toruna
statement

Notifyuserswhoarewatchingthisexercise

Keepothersupdatedbycheckingthisboxwhenmakingsubstantialchanges.Don'tcheckwhenmakingsmallgrammaticalfixes.

FormattingHelpPreview Update Cancel


Edit Update
0Likes

Comment
https://launchschool.com/exercises/a018e581

8/19

12/6/2016

LaunchSchoolanonlineschoolfordevelopers

CreateComment
Commentonthis
solution

FormattingHelpPreview Post Cancel

Edit Post

PhillipDJohnson
2monthsago
defrepeat(word,count)
count.times{putsword}
end

EditSubmission
```ruby
defrepeat(word,
count)

Notifyuserswhoarewatchingthisexercise

Keepothersupdatedbycheckingthisboxwhenmakingsubstantialchanges.Don'tcheckwhenmakingsmallgrammaticalfixes.

FormattingHelpPreview Update Cancel


Edit Update
0Likes

Comment
CreateComment
Commentonthis
solution

FormattingHelpPreview Post Cancel

Edit Post

JiaHungLin
3monthsago
defrepeat(string,num)
num.times{putsstring}
end
repeat("Hello",3)

EditSubmission
```ruby
defrepeat(string,
num)

Notifyuserswhoarewatchingthisexercise

Keepothersupdatedbycheckingthisboxwhenmakingsubstantialchanges.Don'tcheckwhenmakingsmallgrammaticalfixes.

FormattingHelpPreview Update Cancel


Edit Update
0Likes

Comment
CreateComment
Commentonthis
solution

FormattingHelpPreview Post Cancel

Edit Post
https://launchschool.com/exercises/a018e581

9/19

12/6/2016

LaunchSchoolanonlineschoolfordevelopers

AaronCallagher
3monthsago
defrepeat(str,num)
num.timesdo
putsstr
end
end
repeat('Hello',3)

EditSubmission
```
defrepeat(str,
num)

Notifyuserswhoarewatchingthisexercise

Keepothersupdatedbycheckingthisboxwhenmakingsubstantialchanges.Don'tcheckwhenmakingsmallgrammaticalfixes.

FormattingHelpPreview Update Cancel


Edit Update
0Likes

Comment
CreateComment
Commentonthis
solution

FormattingHelpPreview Post Cancel

Edit Post

MichaelWahlin
3monthsago
defrepeat(phrase,num_of)
num_of.times{putsphrase}
end
repeat('hello',3)

EditSubmission
```
defrepeat(phrase,
num_of)

Notifyuserswhoarewatchingthisexercise

Keepothersupdatedbycheckingthisboxwhenmakingsubstantialchanges.Don'tcheckwhenmakingsmallgrammaticalfixes.

FormattingHelpPreview Update Cancel


Edit Update
0Likes

Comment
CreateComment
Commentonthis
solution

FormattingHelpPreview Post Cancel

Edit Post

https://launchschool.com/exercises/a018e581

10/19

12/6/2016

LaunchSchoolanonlineschoolfordevelopers

Uben
3monthsago
defrepeat(str='',num='0')
num.times{putsstr}
end
repeat('hello',3)

EditSubmission
```ruby
defrepeat(str='',
num='0')

Notifyuserswhoarewatchingthisexercise

Keepothersupdatedbycheckingthisboxwhenmakingsubstantialchanges.Don'tcheckwhenmakingsmallgrammaticalfixes.

FormattingHelpPreview Update Cancel


Edit Update
0Likes

Comment
CreateComment
Commentonthis
solution

FormattingHelpPreview Post Cancel

Edit Post

teoorxuan
3monthsago
intrepeats=0
if(repeats!=3){system.out.println("Hello")repeats=repeats+1}
EditSubmission
intrepeats=0;
if(repeats!=3)

Notifyuserswhoarewatchingthisexercise

Keepothersupdatedbycheckingthisboxwhenmakingsubstantialchanges.Don'tcheckwhenmakingsmallgrammaticalfixes.

FormattingHelpPreview Update Cancel


Edit Update
0Likes

Comment
CreateComment
Commentonthis
solution

FormattingHelpPreview Post Cancel

Edit Post

CesareFerrari
3monthsago
Ifthesecondargumentismissingorit'sanotherstring,itprintsoutthefirststringonetimeanyway.

https://launchschool.com/exercises/a018e581

11/19

12/6/2016

LaunchSchoolanonlineschoolfordevelopers

defrepeat(string,number_of_times=1)
num=number_of_times.to_i
ifnum==0
putsstring
else
num.times{putsstring}
end
end
repeat('Hello',3)#=>HelloHelloHello
repeat('Goodbye')#=>Goodbye
repeat('TomorrowisSunday','forever')#=>TomorrowisSunday

EditSubmission
Ifthesecond
argumentismissing
orit'sanother

Notifyuserswhoarewatchingthisexercise

Keepothersupdatedbycheckingthisboxwhenmakingsubstantialchanges.Don'tcheckwhenmakingsmallgrammaticalfixes.

FormattingHelpPreview Update Cancel


Edit Update
1Like

Comment
CreateComment
Commentonthis
solution

FormattingHelpPreview Post Cancel

Edit Post

ToviNewman
3monthsago
defrepeat(string,num)
num.times{putsstring}
end
repeat('Hello',3)

EditSubmission
```ruby
defrepeat(string,
num)

Notifyuserswhoarewatchingthisexercise

Keepothersupdatedbycheckingthisboxwhenmakingsubstantialchanges.Don'tcheckwhenmakingsmallgrammaticalfixes.

FormattingHelpPreview Update Cancel


Edit Update
0Likes

Comment
CreateComment
Commentonthis
solution

FormattingHelpPreview Post Cancel

Edit Post

https://launchschool.com/exercises/a018e581

12/19

12/6/2016

LaunchSchoolanonlineschoolfordevelopers

JimmyAdames
4monthsago
defrepeat(string,num)num.timesdoputsstringendend
repeat('Hello',3)
EditSubmission
defrepeat(string,
num)
num.timesdoputs

Notifyuserswhoarewatchingthisexercise

Keepothersupdatedbycheckingthisboxwhenmakingsubstantialchanges.Don'tcheckwhenmakingsmallgrammaticalfixes.

FormattingHelpPreview Update Cancel


Edit Update
0Likes

Comment
CreateComment
Commentonthis
solution

FormattingHelpPreview Post Cancel

Edit Post

DirkBruins
4monthsago
defrepeat(str,num)
str<<"\n"
puts"#{str*num}"
end

EditSubmission
```ruby
defrepeat(str,
num)

Notifyuserswhoarewatchingthisexercise

Keepothersupdatedbycheckingthisboxwhenmakingsubstantialchanges.Don'tcheckwhenmakingsmallgrammaticalfixes.

FormattingHelpPreview Update Cancel


Edit Update
0Likes

Comment
CreateComment
Commentonthis
solution

FormattingHelpPreview Post Cancel

Edit Post

DerekXu
4monthsago
defrepeat(string,n)total=0whiletotal<ndoputsstringtotal+=1endend
repeat("hello",3)
https://launchschool.com/exercises/a018e581

13/19

12/6/2016

LaunchSchoolanonlineschoolfordevelopers

EditSubmission
defrepeat(string,
n)
total=0

Notifyuserswhoarewatchingthisexercise

Keepothersupdatedbycheckingthisboxwhenmakingsubstantialchanges.Don'tcheckwhenmakingsmallgrammaticalfixes.

FormattingHelpPreview Update Cancel


Edit Update
0Likes

Comment
CreateComment
Commentonthis
solution

FormattingHelpPreview Post Cancel

Edit Post

RenierSoto
4monthsago
defrepeat(str,num)
str*num
end
printrepeat('hi',4)

EditSubmission
```ruby
defrepeat(str,
num)

Notifyuserswhoarewatchingthisexercise

Keepothersupdatedbycheckingthisboxwhenmakingsubstantialchanges.Don'tcheckwhenmakingsmallgrammaticalfixes.

FormattingHelpPreview Update Cancel


Edit Update
0Likes

Comment
CreateComment
Commentonthis
solution

FormattingHelpPreview Post Cancel

Edit Post

Matt
4monthsago
defrepeat(phrase,number)
count=0
untilcount==number
putsphrase
count+=1
end
end
repeat('Hello',3)

https://launchschool.com/exercises/a018e581

14/19

12/6/2016

LaunchSchoolanonlineschoolfordevelopers

EditSubmission
```ruby
defrepeat(phrase,
number)

Notifyuserswhoarewatchingthisexercise

Keepothersupdatedbycheckingthisboxwhenmakingsubstantialchanges.Don'tcheckwhenmakingsmallgrammaticalfixes.

FormattingHelpPreview Update Cancel


Edit Update
0Likes

Comment
CreateComment
Commentonthis
solution

FormattingHelpPreview Post Cancel

Edit Post

MelissaKaulfuss
4monthsago
puts('Hello')*3
EditSubmission
puts('Hello')*3

Notifyuserswhoarewatchingthisexercise

Keepothersupdatedbycheckingthisboxwhenmakingsubstantialchanges.Don'tcheckwhenmakingsmallgrammaticalfixes.

FormattingHelpPreview Update Cancel


Edit Update
0Likes

Comment
CreateComment
Commentonthis
solution

FormattingHelpPreview Post Cancel

Edit Post

Srinath
4monthsago
defrepeat(input,count)
putsinput.concat("\n")*count
end
repeat("Hello",3)
Hello
Hello
Hello

Thisisaquickwaytopullofftheabovetherequirement.ItusesoneofRubyStringclassmethod.
EditSubmission
https://launchschool.com/exercises/a018e581

15/19

12/6/2016

```
defrepeat(input,
count)

LaunchSchoolanonlineschoolfordevelopers

Notifyuserswhoarewatchingthisexercise

Keepothersupdatedbycheckingthisboxwhenmakingsubstantialchanges.Don'tcheckwhenmakingsmallgrammaticalfixes.

FormattingHelpPreview Update Cancel


Edit Update
1Like

Comment
CreateComment
Commentonthis
solution

FormattingHelpPreview Post Cancel

Edit Post

Srinath
4monthsago
defrepeat(input,count)
putsinput.concat("\n")*count
end
repeat("Hello",3)
Hello
Hello
Hello

Thisisaquickwaytopullofftheabovetherequirement.ItusesoneofRubyStringclassmethod.
EditSubmission
```
defrepeat(input,
count)

Notifyuserswhoarewatchingthisexercise

Keepothersupdatedbycheckingthisboxwhenmakingsubstantialchanges.Don'tcheckwhenmakingsmallgrammaticalfixes.

FormattingHelpPreview Update Cancel


Edit Update
0Likes

Comment
CreateComment
Commentonthis
solution

FormattingHelpPreview Post Cancel

Edit Post

bosvrum
4monthsago
defrepeat(string,number)
1.upto(number)do
putsstring
end
end
https://launchschool.com/exercises/a018e581

16/19

12/6/2016

LaunchSchoolanonlineschoolfordevelopers

EditSubmission
```
defrepeat(string,
number)

Notifyuserswhoarewatchingthisexercise

Keepothersupdatedbycheckingthisboxwhenmakingsubstantialchanges.Don'tcheckwhenmakingsmallgrammaticalfixes.

FormattingHelpPreview Update Cancel


Edit Update
1Like

Comment
CreateComment
Commentonthis
solution

FormattingHelpPreview Post Cancel

Edit Post

JoeyFeng
4monthsago
defrepeat(message,number)whilenumber>0putsmessagenumber=number1endend
repeat("sup",3)
EditSubmission
defrepeat(message,
number)

while

Notifyuserswhoarewatchingthisexercise

Keepothersupdatedbycheckingthisboxwhenmakingsubstantialchanges.Don'tcheckwhenmakingsmallgrammaticalfixes.

FormattingHelpPreview Update Cancel


Edit Update
0Likes

Comment
CreateComment
Commentonthis
solution

FormattingHelpPreview Post Cancel

Edit Post

DembaSiby
5monthsago
defrepeat(str,num)
count=0
untilcount==num
putsstr
count+=1
end
end

EditSubmission
```ruby
defrepeat(str,
num)

Notifyuserswhoarewatchingthisexercise

https://launchschool.com/exercises/a018e581

17/19

12/6/2016

LaunchSchoolanonlineschoolfordevelopers

Keepothersupdatedbycheckingthisboxwhenmakingsubstantialchanges.Don'tcheckwhenmakingsmallgrammaticalfixes.

FormattingHelpPreview Update Cancel


Edit Update
1Like

Comment
CreateComment
Commentonthis
solution

FormattingHelpPreview Post Cancel

Edit Post

DembaSiby
5monthsago
defrepeat(str,num)
count=1
loopdo
putsstr
count+=1
breakifcount>num
end
end

EditSubmission
```ruby
defrepeat(str,
num)

Notifyuserswhoarewatchingthisexercise

Keepothersupdatedbycheckingthisboxwhenmakingsubstantialchanges.Don'tcheckwhenmakingsmallgrammaticalfixes.

FormattingHelpPreview Update Cancel


Edit Update
1Like

Comment
CreateComment
Commentonthis
solution

FormattingHelpPreview Post Cancel

Edit Post

DavidKhokhar
5monthsago
defrepeat2(word,number_of_times)
puts(word+"\n")*number_of_times
end

Interestingly,Ifoundyouneedtouse""andnot''tointerpretescapecharacterssuchas\ncorrectly
EditSubmission
```ruby
defrepeat2(word,
number_of_times)

Notifyuserswhoarewatchingthisexercise

Keepothersupdatedbycheckingthisboxwhenmakingsubstantialchanges.Don'tcheckwhenmakingsmallgrammaticalfixes.
https://launchschool.com/exercises/a018e581

18/19

12/6/2016

LaunchSchoolanonlineschoolfordevelopers

FormattingHelpPreview Update Cancel


Edit Update
2Likes

Comment
CreateComment
Commentonthis
solution

FormattingHelpPreview Post Cancel

Edit Post

Zach
5monthsago
defrepeat(string,num)
num.times{pstring}
end

EditSubmission
```
defrepeat(string,
num)

Notifyuserswhoarewatchingthisexercise

Keepothersupdatedbycheckingthisboxwhenmakingsubstantialchanges.Don'tcheckwhenmakingsmallgrammaticalfixes.

FormattingHelpPreview Update Cancel


Edit Update
4Likes

Comment
CreateComment
Commentonthis
solution

FormattingHelpPreview Post Cancel

Edit Post

FormattingHelp
SupportsGithubFlavoredMarkdown
**bold**,~~strikethrough~~`singlelineofcode`
SupportsEmoji
Use```rubybeforeacodeblockandendwith```,forexample:
```ruby
classFoo
end
```

https://launchschool.com/exercises/a018e581

19/19

You might also like