Bird
Raised Fist0
Rubyprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the Ruby method split do when called on a string?
easy
A. It divides the string into an array based on a separator.
B. It combines array elements into a single string.
C. It changes all characters to uppercase.
D. It removes spaces from the string.

Solution

  1. Step 1: Understand the purpose of split

    The split method breaks a string into parts wherever a separator is found.
  2. Step 2: Identify the output type

    After splitting, the output is an array containing the separated parts of the string.
  3. Final Answer:

    It divides the string into an array based on a separator. -> Option A
  4. Quick Check:

    split = divide string into array [OK]
Hint: Split cuts strings into arrays at separators [OK]
Common Mistakes:
  • Thinking split joins array elements
  • Confusing split with uppercase conversion
  • Assuming split removes spaces without separator
2. Which of the following is the correct syntax to join an array ["a", "b", "c"] into a string separated by dashes in Ruby?
easy
A. ["a", "b", "c"].split("-")
B. ["a", "b", "c"].join("-")
C. "a-b-c".join(["a", "b", "c"])
D. "a-b-c".split(["a", "b", "c"])

Solution

  1. Step 1: Identify the method to combine array elements

    The join method is used on arrays to create a string with a separator.
  2. Step 2: Check the syntax for join with a separator

    The syntax is array.join(separator), so ["a", "b", "c"].join("-") is correct.
  3. Final Answer:

    ["a", "b", "c"].join("-") -> Option B
  4. Quick Check:

    join(array) with separator = correct syntax [OK]
Hint: Use array.join(separator) to combine elements [OK]
Common Mistakes:
  • Using split instead of join to combine
  • Calling join on a string instead of array
  • Passing array as argument to split or join incorrectly
3. What is the output of this Ruby code?
sentence = "apple,banana,cherry"
fruits = sentence.split(",")
result = fruits.join(" & ")
puts result
medium
A. apple & banana & cherry
B. apple,banana,cherry
C. apple banana cherry
D. ["apple", "banana", "cherry"]

Solution

  1. Step 1: Split the string by commas

    The split(",") breaks the string into ["apple", "banana", "cherry"].
  2. Step 2: Join the array with " & " separator

    The join(" & ") combines the array elements into the string "apple & banana & cherry".
  3. Final Answer:

    apple & banana & cherry -> Option A
  4. Quick Check:

    split then join with & = apple & banana & cherry [OK]
Hint: Split by comma, join with & gives 'apple & banana & cherry' [OK]
Common Mistakes:
  • Expecting original string without changes
  • Confusing split and join order
  • Thinking join adds commas automatically
4. The following Ruby code is intended to split a string into words and then join them with a space, but it raises an error. What is the problem?
text = "hello world"
words = text.split
joined = words.join
puts joined()
medium
A. The variable words is not an array.
B. The method split requires an argument.
C. The call joined() is incorrect syntax.
D. The join method cannot be called without a separator.

Solution

  1. Step 1: Check the method calls

    split without arguments splits on whitespace correctly. join can be called without arguments to join with empty string.
  2. Step 2: Identify the error in puts joined()

    In Ruby, calling a variable like a method with parentheses joined() causes an error because joined is a string, not a method.
  3. Final Answer:

    The call joined() is incorrect syntax. -> Option C
  4. Quick Check:

    Calling string like method with () = error [OK]
Hint: Don't add () when printing variables in Ruby [OK]
Common Mistakes:
  • Thinking split needs an argument always
  • Assuming join requires a separator
  • Calling variables as methods with parentheses
5. Given a string data = "red;green;blue;yellow", write Ruby code to split it into an array, remove the color "blue", and then join the remaining colors with commas. What is the correct output?
hard
A. "red;green;yellow"
B. "red,green,blue,yellow"
C. "red green yellow"
D. "red,green,yellow"

Solution

  1. Step 1: Split the string by semicolons

    Using split(";") on data gives ["red", "green", "blue", "yellow"].
  2. Step 2: Remove "blue" from the array

    Remove "blue" by using array.delete("blue") or array.reject!.
  3. Step 3: Join the remaining elements with commas

    Joining with join(",") results in "red,green,yellow".
  4. Final Answer:

    "red,green,yellow" -> Option D
  5. Quick Check:

    Split by ;, remove blue, join by , = red,green,yellow [OK]
Hint: Split by ; remove item, join by , for final string [OK]
Common Mistakes:
  • Forgetting to remove "blue" before joining
  • Joining with wrong separator
  • Using split without argument or wrong separator
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard