Splitting and Joining
split, join, chars.
Splitting and Joining is a free Ruby Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Splitting a String
The split method breaks a string into an array using a separator. By default it splits on whitespace.
sentence = "the quick brown fox"
words = sentence.split
puts words.inspectSplitting on a Delimiter
Pass any string as the separator, such as a comma for CSV-style data.
csv = "red,green,blue"
puts csv.split(",").inspectLimiting Splits
A second argument limits how many pieces you get. The rest stays in the last element.
data = "a:b:c:d"
puts data.split(":", 2).inspectSplitting with a Regex
Split on a pattern when separators vary. /\s+/ splits on one or more spaces.
messy = "one two three"
puts messy.split(/\s+/).inspectSplitting into Characters
Splitting on an empty string gives individual characters, but chars is the idiomatic way.
word = "ruby"
puts word.chars.inspectJoining an Array
The array join method is the opposite of split. It glues elements together with a separator.
parts = ["2024", "05", "30"]
puts parts.join("-")Join with No Separator
Calling join without an argument concatenates with nothing between elements.
letters = ["r", "u", "b", "y"]
puts letters.joinRound Trip
Split then join lets you transform text. Here we reverse the order of words.
sentence = "hello there friend"
puts sentence.split.reverse.join(" ")Counting Words
Split gives an array, so you can use length to count words.
text = "one two three four"
puts text.split.lengthWorking with Lines
lines splits on newlines and keeps them, while each_line iterates. split("\n") drops the newlines.
text = "first\nsecond\nthird"
puts text.split("\n").lengthCleaning Split Results
Combine split with map and strip to clean spacey input.
raw = "apple , banana , cherry"
clean = raw.split(",").map(&:strip)
puts clean.inspectQuick Check
Check your split and join knowledge.
Recap
You learned to break apart and rebuild text:
splitwith strings, regex, or a limitcharsfor individual characters- array
jointo rebuild with a separator - split-map-join pipelines for cleaning data
Next course: symbols and hashes.
puts "x,y,z".split(",").join(" ")Frequently asked questions
Is the “Splitting and Joining” lesson free?
Yes — the full text of “Splitting and Joining” is free to read here on the web, and the Ruby Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ruby Academy course, upgrade to CoddyKit PRO.
What will I learn in “Splitting and Joining”?
split, join, chars. You practise Ruby Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Ruby Academy?
No prior experience is required. Ruby Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Splitting and Joining” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Ruby Academy lesson?
Yes. Every Ruby Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- String Basics and Methods
- Interpolation and Formatting
- Searching and Replacing
- Splitting and Joining