Ruby from the ground up
A language designed to make programmers happy
- Beginner
- 13 chapters
- 224 min read
Learn Ruby by writing it: values and variables, text, decisions, loops and ranges, arrays, hashes, blocks — the idea Ruby is built around — methods, classes, modules and mixins, errors and the standard library, ending with a small library that uses all of it.
What You'll Learn
- Print output three ways and interpolate values into strings
- Know that everything is an object, and ask values what they are
- Tidy text with strip, split, join, gsub and format
- Write conditions in Ruby, where only nil and false are false
- Repeat work with while, until, ranges and each — and steer it with next and break
- Question arrays with map, select, reject, sort and sum instead of loops
- Store records and counts in hashes, with symbols as keys
- Write and yield blocks, and use sort_by, group_by, inject and tally
- Name work as methods, with defaults, splats, keyword arguments and guard clauses
- Build classes with state, attributes and to_s, and inherit with super
- Share behaviour with modules and get the whole of Enumerable from one method
- Catch and raise exceptions, and use the standard library — JSON, dates, sets
- Assemble a small library with classes, a mixin, blocks, errors and JSON
Prerequisites
- No programming experience needed — this course starts at the first line
- Any other language you know will make the early chapters quick
Course Content
- 01Your first RubyNot started
What Ruby is, printing output three ways, numbers that behave, and the string interpolation you will use in every program after this one.
- 02Values, and the names you give themNot started
Ruby has no primitives: everything is an object you can ask questions of. Numbers, text, symbols, nil, and the conversions between them.
- 03Text, and the methods it bringsNot started
Strings are objects with a large vocabulary: strip, split, join, gsub, the word-list literal, and format for lining numbers up in columns.
- 04Decisions, and what counts as trueNot started
if, elsif and unless, the modifiers that read like English, case for many branches, and Ruby's honest surprise: only nil and false are false.
- 05Repetition, ranges, and saying no to a loopNot started
while and until, the times shortcut, each over a range, and the two words that steer a loop from inside: next and break.
- 06Arrays: ordered lists you can questionNot started
Building lists, reaching into them, and the methods that turn "loop over this and collect" into a single readable line.
- 07Hashes: values with namesNot started
Key and value pairs, symbols as keys, fetch with a default, and the counting pattern that hashes were made for.
- 08Blocks: code you hand to a methodNot started
The idea Ruby is built around — a method calls your block when it is ready — plus the collection toolbox that blocks unlock: sort_by, group_by, inject and tally.
- 09Methods: naming a piece of workNot started
Defining methods, the many shapes of an argument list, the return value that is already there, and the naming conventions that carry meaning.
- 10Classes: values that carry behaviourNot started
A blueprint with state inside: initialize, instance variables, the attribute shortcuts, to_s, and inheritance with super.
- 11Modules: shared behaviour, and names for thingsNot started
A module cannot be built, and that is the point: it holds methods to be mixed into classes, and it gives namespaces so two Items can live in one program.
- 12Errors, and the library you already haveNot started
Exceptions as objects you can catch and raise, the begin/rescue/ensure shape, and a look at the standard library that ships with Ruby.
- 13Putting it together: a small libraryNot started
A capstone that uses almost everything: classes with state, a mixin, Enumerable, blocks, sorted and grouped reports, a custom error, and JSON out of the standard library.
