0
0
Rubyprogramming~10 mins

String concatenation and << 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 concatenate two strings using the + operator.

Ruby
greeting = "Hello, " [1] "world!"
Drag options to blanks, or click blank then click option'
A*
B<<
C+
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using << instead of + will modify the original string instead of creating a new one.
Using * or - operators will cause errors or unexpected results.
2fill in blank
medium

Complete the code to append a string to another string using the << operator.

Ruby
name = "Ruby"
name [1] " is fun!"
Drag options to blanks, or click blank then click option'
A<<
B+
C*
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using + creates a new string instead of modifying the original.
Using * or - will cause errors or unexpected results.
3fill in blank
hard

Fix the error in the code to correctly concatenate strings using <<.

Ruby
message = "Hi"
message [1]= " there!"
Drag options to blanks, or click blank then click option'
A+
B<<
C+=
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using += works but creates a new string instead of modifying the original.
Using - causes syntax errors.
4fill in blank
hard

Complete the code to create a hash where keys are words and values are their lengths using string concatenation.

Ruby
words = ["cat", "dog", "bird"]
lengths = {word: for word in words if word[1] 3}
Drag options to blanks, or click blank then click option'
A:
B>
C<
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using == instead of > will select only words exactly 3 letters long.
Using < will select shorter words, not longer.
5fill in blank
hard

Fill all three blanks to build a hash with uppercase keys and values greater than 3.

Ruby
words = ["apple", "bat", "carrot"]
result = {word[1] word.length for word in words if word.length [2] 3 and word[3] "a"}
Drag options to blanks, or click blank then click option'
A.upcase
B>
C.include?
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > will select shorter words.
Using == instead of .include? will cause errors.