Advanced Ruby: a 2nd-level course. If you have already done some Ruby programming, this course will help to take your coding to the next level. It explains some of the more advanced features of Ruby including blocks and procs, threads, regular expressions, exception handling, singletons and metaprogramming. The course is based on Huw Collingbourne's book, The Book Of Ruby (a copy of 3rd PDF edition of the book is supplied with this course). All the source code is provided in the code archive. This course can be used with any editor or IDE and on any operating system that supports Ruby. If you are ready to go beyond the basics and gain a deeper understanding of how Ruby really works, this course is for you.
Note: If you're new to programming, check out Ruby Programming for Beginners (now with over 24,000 students enrolled!).
What is this course about and who is it for? Watch this video to find out.
This is an intermediate-to-advanced level course on Ruby programming. It can be used with any code editor or IDE and any operating system that supports Ruby. This video provides a quick overview of the course and gives a few tips on how to get the most from it.
This is the 'course text' – a short eBook that summarizes the most important topics from each of the steps of this course. Use this book to revise the subjects. It also contains links to useful resources such as Ruby installers, editors and IDEs.
This contains an archive of all the code to accompany The Book Of Ruby and this course
Table of Contents
Chapter 1: Strings, Numbers, Classes, and Objects
Chapter 2: Class Hierarchies, Attributes, and Class Variables
Chapter 3: Strings and Ranges
Chapter 4: Arrays and Hashes
Chapter 5: Loops and Iterators
Chapter 6: Conditional Statements
Chapter 7: Methods
Chapter 8: Passing Arguments and Returning Values
Chapter 9: Exception Handling
Chapter 10: Blocks, Procs, and Lambdas
Chapter 11: Symbols
Chapter 12: Modules and Mixins
Chapter 13: Files and IO
Chapter 14: YAML
Chapter 15: Marshal
Chapter 16: Regular Expressions
Chapter 17: Threads
Chapter 18: Debugging and Testing
Chapter 19: Ruby on Rails
Chapter 20: Dynamic Programming
Appendix A: Documenting Ruby with RDoc
Appendix B: Installing MySQL for Ruby on Rails
Appendix C: Further Reading
Appendix D: Web sites
Appendix E: Ruby Development Software
An overview of how to test conditions in Ruby and a first look at the alternative Boolean operators: and/&&, or/||, !/not
An explanation of why the same tests may produce different results if you change the style of Boolean operators. Also a look at the various types of if, else and unless tests, plus case statements.
Performing tests in Ruby may seem easy. But there are some complexities which, unless you are very careful, can change the way your program works. Use this quiz to test your understanding...
What are class methods and what are they used for?
How to use class methods and class variables and why it’s important to understand that classes are objects too!
How to add methods to a specific object rather than to an entire class
The difference between class and instance methods and variables, plus singletons.
Regular arguments and default arguments, a discussion of parentheses, single and multiple return values and parallel assignment
Usually, there is one way to pass data to a method in a Ruby object and one way to get data back out again. But there are exceptions to the rule. Here I discuss the importance of encapsulation and information hiding.
Information-hiding, returning values from methods and unintentional side-effects
Trapping exceptions and recovering from errors in your code. The fundamentals of exception handling techniques in Ruby.
Exceptions are objects and they have a class hierarchy which can be useful to you when handling specific types of error
How to deal with errors
In Ruby blocks are like ‘nameless methods’ that are often used in iterators. This short video explains the basics
Blocks, procs, lambdas, block parameters, closures, passing blocks as arguments, yielding anonymous blocks and using blocks as iterators
How to pass and execute blocks to methods. Careful! This is quite tricky!
Symbols are widely used in Ruby programming. Here we find out what’s special about them
This is Chapter 11 of The Book Of Ruby. It explains symbols.
Threads and Fibers can help you to write programs that do more than one thing at a time. But they can be tricky things to use effectively!
What are Ruby Symbols? And how can threads help your programs multitask?
Ruby does not implement ‘multiple inheritance’. So how can a class include features from multiple parent classes? The answer is: it can’t. But it can mix-in multiple modules…
You just have to look at a module definition to see how similar it is to a class. But the resemblance is not superficial. Here I explain the relationship between a Class and a Module.
Modules as namespaces and as mixins. How to access module constants, module methods and instance methods and how to avoid potential problems
Modules, mixins and methods
How to use Ruby's File and IO classes to read, write and copy files. How to find file information and traverse directories and subdirectories recursively.
How to save and load structured data to and from disk in a human readable format.
You can use Marshal to read and write byte streams of data to and from disk
Opening and closing files, reading and writing data
This explains the fundamentals of matching text patterns to find or modify strings using Regular Expressions
Some sample programs show how you can match patterns substitute text or create new files – for example, containing extracted documentation – using Regular Expressions
Matching and replacing text patterns
How to add and remove methods at runtime, deal with method-calls to methods that don’t exist and evaluate strings as Ruby code.
In Ruby you don’t have to write a complete program before you run it. You can add new Ruby code to the program that is running. Here’s a simple example.
Blurring the boundaries between code and data
Huw Collingbourne is the technology director at SapphireSteel Software, developers of programming tools for Microsoft Visual Studio. He is author of The Book Of Ruby from No Starch Press. He runs Bitwise Courses and teaches courses on a range of programming topics.
Huw has been a programmer for more than 30 years. He is a well-known technology writer in the UK. For over ten years he wrote the Delphi and Java programming column for PC Plus Magazine. He has also written numerous opinion and programming columns (including tutorials on C#, C++, Smalltalk and Ruby) for a number of computer magazines, such as Computer Shopper, PC Pro, and PC Plus.
In the 1980s he was a pop music journalist and interviewed most of the New Romantic stars, such as Duran Duran, Spandau Ballet, Adam Ant, Boy George, and Depeche Mode. He is now writing a series of New Romantic murder mysteries.
At various times Huw has been a magazine publisher, editor, and TV broadcaster. He has an MA in English from the University of Cambridge and holds a 2nd dan black belt in aikido, a martial art which he teaches in North Devon, UK. The aikido comes in useful when trying (usually unsuccessfully) to keep his Pyrenean Mountain Dogs under some semblance of control.