0
0
Rubyprogramming~20 mins

Split and join methods in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Split and Join Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of splitting a string with multiple spaces
What is the output of this Ruby code?
Ruby
text = "apple  banana   cherry"
result = text.split(" ")
puts result.inspect
A["apple", " banana", " cherry"]
B["apple", "banana", "cherry"]
C["apple banana cherry"]
D["apple", "", "banana", "", "", "cherry"]
Attempts:
2 left
💡 Hint
The split method with a space splits exactly on each space character.
Predict Output
intermediate
2:00remaining
Joining array elements with a custom separator
What is the output of this Ruby code?
Ruby
arr = ["red", "green", "blue"]
result = arr.join("-")
puts result
Ared green blue
Bred,green,blue
Cred-green-blue
Dredgreenblue
Attempts:
2 left
💡 Hint
The join method uses the string you give as a separator between elements.
🔧 Debug
advanced
2:00remaining
Why does this split produce an empty first element?
Consider this Ruby code. Why does the first element of the result array become an empty string?
Ruby
text = ",one,two,three"
result = text.split(",")
puts result.inspect
ABecause the string starts with the separator, split creates an empty string before the first comma.
BBecause split removes the first element if it is empty.
CBecause split ignores separators at the start of the string.
DBecause split treats commas as part of the first element.
Attempts:
2 left
💡 Hint
Think about what happens before the first comma.
Predict Output
advanced
2:00remaining
Splitting with limit parameter
What is the output of this Ruby code?
Ruby
text = "a,b,c,d"
result = text.split(",", 3)
puts result.inspect
A["a", "b", "c", "d"]
B["a", "b", "c,d"]
C["a,b", "c", "d"]
D["a", "b,c", "d"]
Attempts:
2 left
💡 Hint
The limit parameter controls how many parts the string is split into.
🧠 Conceptual
expert
3:00remaining
Effect of split and join on string with multiple separators
Given the Ruby code below, what is the final output?
Ruby
text = "one,,two,,,three"
parts = text.split(",")
joined = parts.join(",")
puts joined
Aone,,two,,,three
Bone,two,three
Cone,,two,,three
Done,two,,three
Attempts:
2 left
💡 Hint
Think about how split handles consecutive separators and what join does with empty strings.