Skip to content
Justice Tech School

Ruby Tutorial

A readable, friendly language loved for web apps and scripting.

Start learning Ruby
Getting started4 lessons
  1. 1Ruby IntroductionReadable and fun.
  2. 2Ruby Get StartedInstall Ruby with your package manager, rbenv or the Windows installer.
  3. 3Ruby SyntaxRuby needs no semicolons or parentheses for most calls.
  4. 4Ruby Comments# starts a single line comment.
Basics20 lessons
  1. 5Variables and StringsDynamic typing and interpolation.
  2. 6Numbers and ConditionsMath and branching.
  3. 7Ruby VariablesVariables need no declaration.
  4. 8Ruby Data TypesCore types: Integer, Float, String, Symbol, Array, Hash, Range, true, false and nil.
  5. 9Ruby Type Conversionto_i, to_f, to_s, to_a and to_sym convert values.
  6. 10Ruby Input and Outputputs prints a line, print does not add a newline, p shows an inspected value and gets read.
  7. 11Ruby Booleans and Comparisontrue and false are objects.
  8. 12Ruby SymbolsA symbol is an immutable name written :name.
  9. 13Constants, Globals and Special VariablesConstants start with a capital letter.
  10. 14Everything is an ObjectEven numbers and nil are objects with methods.
  11. 15Variables and ConstantsLocal variables start with a lowercase letter.
  12. 16Everything Is an ObjectNumbers, strings and nil are objects with methods.
  13. 17NumbersIntegers are unlimited.
  14. 18StringsUse single or double quotes.
  15. 19String Methodsupcase, downcase, capitalize, strip, split, gsub, include?, start_with?, reverse and cente.
  16. 20SymbolsA symbol is a lightweight unchangeable name like :ok.
  17. 21nil and TruthinessOnly false and nil are falsy.
  18. 22OperatorsArithmetic, comparison, logical, ranges and the spaceship <=>.
  19. 23puts, print and pputs adds a newline, print does not and p shows the inspect form.
  20. 24Type Conversionto_i, to_f, to_s, to_a and Integer() convert values.
Strings and numbers9 lessons
  1. 25Ruby Strings In DepthStrings are mutable.
  2. 26Heredocs and FormattingA heredoc holds multi line text.
  3. 27Ruby NumbersIntegers have no size limit.
  4. 28Random and Timerand and Random give random numbers, and Time handles dates.
  5. 29Heredocs and formatHeredocs write long text.
  6. 30Regular Expressions=~ and match test patterns.
  7. 31Formatting Numbersround, floor, ceil and to_s with padding format numbers.
  8. 32TimeTime.now, Time.new and strftime handle dates.
  9. 33Math and RandomMath has sqrt, sin, PI and more.
Control flow10 lessons
  1. 34Ruby If, Unless and Ternaryif and elsif choose branches, unless runs when a condition is false, and statement modifie.
  2. 35Ruby Casecase matches a value with when clauses using ===.
  3. 36While, Until and Loopwhile repeats while true, until repeats until true and loop do runs forever until break.
  4. 37Ruby Iteratorseach, times, upto, downto, step and each_with_index cover almost all loops.
  5. 38if, elsif, else and unlessConditions can end with a modifier.
  6. 39case Expressionscase matches a value against when clauses, including ranges, classes and regexes.
  7. 40Loopswhile, until, loop do, times, upto and step repeat code.
  8. 41each, map and selectIterate with each, transform with map and filter with select and reject.
  9. 42break, next and redobreak exits a loop, next skips to the next round.
  10. 43Rangesa..b includes b, a...b excludes it.
Collections15 lessons
  1. 44Arrays and HashesLists and dictionaries.
  2. 45Loops and Iteratorseach, times and while.
  3. 46Ruby Arrays In DepthArrays grow automatically.
  4. 47Array Methods: map, select, reducemap transforms, select and reject filter, reduce combines, sum adds, min and max find extr.
  5. 48Ruby Hashes In DepthA hash maps keys to values.
  6. 49Ranges and SetsRanges represent sequences, and to_a expands them.
  7. 50Enumerable and LazyAny class that defines each and includes Enumerable gets map, select, sort and more.
  8. 51ArraysArrays grow automatically.
  9. 52Array Methodssort, reverse, uniq, flatten, compact, include?, index, sample and zip.
  10. 53sort_by, min_by, group_bysort_by, min_by, max_by and group_by work with a block.
  11. 54HashesHashes map keys to values.
  12. 55Hash Methodsselect, map, transform_values, merge, sort_by, to_a and each_with_object.
  13. 56Counting and Tallyingtally counts items and count counts matches.
  14. 57SetSet stores unique values.
  15. 58Destructuring and SplatAssign several values at once, use * to collect the rest and ** for keywords.
Methods and blocks11 lessons
  1. 59Method ArgumentsMethods accept default values, keyword arguments, splat *args and double splat **opts.
  2. 60Blocks, Yield and ProcsA block is code passed to a method.
  3. 61Closures and Method ObjectsBlocks and lambdas capture the variables around them.
  4. 62Ruby Recursion and MemoizationA method can call itself.
  5. 63Methodsdef defines a method.
  6. 64Blocks and yieldA block is code passed to a method.
  7. 65Procs and LambdasProcs and lambdas are saved blocks.
  8. 66Variable Arguments*args collects extra arguments and **opts collects keywords.
  9. 67Method Chaining and Symbols to ProcChain methods and use &:method as a shortcut for a block.
  10. 68RecursionA method can call itself.
  11. 69Variable ScopeMethods have their own scope.
Methods and classes3 lessons
  1. 70Methods and BlocksDefining behaviour.
  2. 71Classes and ModulesObjects the Ruby way.
  3. 72Errors and Filesrescue and File.
Object oriented Ruby14 lessons
  1. 73Ruby InheritanceA class inherits from one parent with <.
  2. 74Modules and MixinsA module groups methods and constants.
  3. 75Public, Private and ProtectedMethods are public by default.
  4. 76Class Methods and Constantsdef self.name defines a class method.
  5. 77Struct, Comparable and to_sStruct builds small data classes.
  6. 78Duck Typing and PolymorphismRuby cares what an object can do, not what class it is.
  7. 79ClassesA class holds data and behaviour.
  8. 80attr_reader, writer and accessorattr_* define getters and setters in one line.
  9. 81Inheritance and super< inherits.
  10. 82Modules and MixinsA module groups methods.
  11. 83Class Methods and Variablesdef self.name makes a class method.
  12. 84public, private, protectedprivate methods are only callable inside the object.
  13. 85to_s, inspect and ComparableDefine to_s for printing and include Comparable plus <=> for ordering.
  14. 86StructStruct makes a small class with accessors in one line.
Errors and files12 lessons
  1. 87Ruby Exceptionsbegin, rescue, else, ensure and retry handle errors.
  2. 88Ruby File I/OFile.open with a block closes the file automatically.
  3. 89JSON and YAMLThe json library converts Ruby hashes and arrays to text with to_json and JSON.parse back.
  4. 90Regular ExpressionsRegexes live between slashes.
  5. 91begin, rescue, ensureHandle errors with rescue.
  6. 92Custom ErrorsSubclass StandardError for your own errors.
  7. 93Reading and Writing FilesFile.write, File.read and File.foreach handle files.
  8. 94JSONrequire json then to_json and JSON.parse convert data.
  9. 95Enumerable and each_sliceClasses with each can include Enumerable to get map, select, sort and more.
  10. 96Practice: Word FrequencySplit, downcase and tally.
  11. 97Practice: FizzBuzzCombine ranges and case.
  12. 98Practice: Simple Bank AccountCombine classes, errors and formatting.
Advanced RubyPremium5 lessons
  1. 99Metaprogramming BasicsRuby code can write code.
  2. 100Freezing, Immutability and Dupfreeze makes an object read only.
  3. 101Pattern Matchingcase/in matches structure: arrays, hashes and classes.
  4. 102Threads, Mutex and FibersThread runs code concurrently, Mutex protects shared data and Queue passes messages.
  5. 103Operator Overloading and DSLsDefine operators like +, ==, [] and << on your own classes.
Ruby in practicePremium8 lessons
  1. 104Gems and BundlerA gem is a packaged Ruby library.
  2. 105Testing with RSpec and MinitestMinitest ships with Ruby, and RSpec reads like English.
  3. 106Web Apps with SinatraSinatra is a tiny web framework: each route is a block.
  4. 107Ruby on Rails OverviewRails is a full stack framework with models, views and controllers.
  5. 108Active Record BasicsActive Record turns tables into classes.
  6. 109Command Line ScriptsARGV holds arguments, gets reads input, and OptionParser parses flags.
  7. 110Performance and StylePrefer built in methods, avoid repeated work, use symbols and each_with_object, and profil.
  8. 111Ruby Best PracticesWrite small methods, name things clearly, prefer composition, keep classes focused, test e.
Next steps2 lessons
  1. 112What Next?Where to go after the basics.
  2. 113Practice ProjectsBuild a command line todo app, a text adventure, a web scraper with Nokogiri, a Sinatra AP.