0
0
Rubyprogramming~10 mins

Bang methods (ending with !) in Ruby - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to use the bang method that reverses the string in place.

Ruby
str = "hello"
str.[1]
Drag options to blanks, or click blank then click option'
Areverse
Breverse!
Cupcase
Dcapitalize
Attempts:
3 left
💡 Hint
Common Mistakes
Using the non-bang method reverse which returns a new string but does not modify the original.
Using unrelated methods like upcase or capitalize.
2fill in blank
medium

Complete the code to remove trailing whitespace from the string using a bang method.

Ruby
text = "hello   "
text.[1]
Drag options to blanks, or click blank then click option'
Astrip!
Bstrip
Cchomp
Dchomp!
Attempts:
3 left
💡 Hint
Common Mistakes
Using strip which returns a new string but does not change the original.
Using chomp which removes newline characters, not whitespace.
3fill in blank
hard

Fix the error by choosing the correct bang method to convert the string to uppercase in place.

Ruby
name = "alice"
name = name.[1]
Drag options to blanks, or click blank then click option'
Aupcase
Bcapitalize!
Cupcase!
Dcapitalize
Attempts:
3 left
💡 Hint
Common Mistakes
Using upcase which returns a new string but does not modify the original.
Using capitalize! which only capitalizes the first letter.
4fill in blank
hard

Fill both blanks to remove all exclamation marks from the string in place and then reverse it in place.

Ruby
msg = "Wow!!!"
msg.[1]("!")
msg.[2]
Drag options to blanks, or click blank then click option'
Adelete!
Bdelete
Creverse!
Dreverse
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-bang methods which do not modify the original string.
Using delete without the bang, which returns a new string.
5fill in blank
hard

Fill all three blanks to replace 'cat' with 'dog' in place, then upcase the string in place, and finally remove trailing spaces in place.

Ruby
sentence = "the cat is cute  "
sentence.[1]("cat", "dog")
sentence.[2]
sentence.[3]
Drag options to blanks, or click blank then click option'
Agsub!
Bupcase!
Crstrip!
Dgsub
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-bang methods which return new strings but do not modify the original.
Mixing up gsub and gsub!.