Build useful things with hashes, files, JSON, errors and your own classes. Every new word explained.
- 28 lessons
- 7 units
- 4 h 28 m in all
- £0, for good
what’s inside.
For people who finished Level 1, or can already write small programs with variables, decisions, loops and functions.
Ruby Level 2 picks up where Level 1 ended and moves faster, with realistic data and small real programs. In 28 lessons you work with hashes and symbols, Enumerable, tidy text, files, JSON, errors, classes, modules and the standard library, each with a guess, a task and a bug to fix. It ends with a level check: an expense tracker that saves its data as JSON and never crashes on bad input.
every lesson goes the same way.
- meet it
- guess first
- your turn
- fix it
- nailed it
unit 1
Hashes and symbols
Look things up by name instead of by number.
- Making a hash9 minStore values under names and look them up in a flash.
- Symbols make good keys8 minUse short, tidy symbol keys and spot when a lookup goes wrong.
- Changing a hash9 minAdd, update and remove pairs, and ask for a key safely.
- Looping over a hash10 minVisit every pair, add up the values and count things as they come.
unit 2
More Enumerable
Change, filter, total and sort whole collections at once.
- Change every item with map8 minTurn one array into a new one in a single line.
- Keep some, drop some9 minFilter a list down to the items you want, or find the first match.
- Boil it down with reduce10 minSquash a whole list into one answer, like a total or a product.
- Sort, group and chain11 minPut things in order, split them into groups and link methods together.
unit 3
Strings with style
Cut text up, tidy it and line it up neatly.
- Split and join8 minCut a line of text into pieces, work on them, and glue them back.
- Numbers that look right9 minRound numbers and show money with exactly two decimal places.
- Line it up9 minPad text into neat columns and print a tidy receipt.
- Text with many lines10 minWrite long text over several lines and work through it line by line.
unit 4
Files and JSON
Save data in files and read it back, as plain text or as JSON.
- Write it down, read it back8 minSave text in a file and load it again.
- A file of lines10 minAdd to a file a line at a time and read it back line by line.
- JSON turns data into text10 minTurn hashes and arrays into text you can save, and back again.
- Data inside data11 minDig through nested JSON, like a reply from a weather app.
unit 5
When things go wrong
Catch errors, recover from them and raise your own.
- Catch it with rescue9 minStop an error from crashing your program and carry on.
- Rescue the right error10 minCatch only the errors you expect, each with its own answer.
- Always tidy up with ensure10 minRun tidy-up code every time, whether things went well or badly.
- Raise your own errors10 minStop bad values at the door with a clear message.
unit 6
Your own classes
Design your own kinds of value with classes.
- Making a class9 minDesign your own kind of value and make as many as you like.
- Objects that remember10 minGive every object its own name, numbers and memory.
- Readers, writers and to_s10 minLet other code read and change an object's data, and print it nicely.
- Inheritance11 minBuild a new class on top of one you already have.
unit 7
Modules and the toolbox
Mix in shared skills and use Ruby's own toolbox.
- Your own kind of error10 minMake error classes that say exactly what went wrong in your program.
- Modules hold tools9 minKeep related methods and fixed values together under one name.
- Mix in Comparable11 minTeach your objects to be compared, sorted and ranked.
- The standard library and gems10 minUse the tools that come with Ruby and learn where the rest come from.