Modern Ruby
This course will show you code samples using the new code syntax and help you decide if and when you want to use it or how it might fit your workflow.
I will generally focus on providing production code that uses the new language features and not just teaching constructs.
All information will be linked to the original documentation and discussions in the Ruby issue tracker or Github and include a curated list of extra resources to read in case anyone wants to dive deeper into a specific subject.
Here is the list that I will cover in this course:
- Numbered Block Params
- Endless method definition
- Hash literal value omission and omitting keyword arguments value in method calls
- Pattern Matching
- Writing code object shapes friendly
- What’s new in core classes Stdlib
Using the new features, you could express your thoughts in Ruby code closer to how you think about the solution and, in most cases, more English-friendly, thus making it easier to understand.
Table of contents
Get a look at all of the content covered in this email course.
It includes the major features and new syntax added to Ruby in the last 4 years with code samples and
explanations.
See here the list of email and the order of the topics covered in each one.
-
Numbered Block Params
Numbered params are implicitly defined block parameters intended to simplify writing short blocks. (source)
Currently (up to Ruby 3.3), names from
_1
to_9
are supported to be used as numbered block params. Ruby 3.4 will introduceit
as an alternative for_1
.Email content
You will see code samples where numbered block params seem to fit and some cases where it can be harder to understand what’s happening.
I will compare before and after code and analyze cases where the readability increases, or is hurt by using the numbered block params. -
Endless Method definition
A shorthand syntax for methods consisting of exactly one expression (source)
An endless method is a way to write a Ruby method on a single line with a syntax like
def name = body
without the need to use theend
keyword.Email content
You will see a code sample where I think the endless method helps make the code concise and easy to read. I explain when not to use it and when it will do something different than intended due to the operators’ priority.
You will read before and after code to decide when to use it and when not to. I will share with examples the three benefits I discovered about endless methods: keeping small utility methods small, protecting against method gravity, and helping thinking in small incremental changes.
-
Hash literal value omission
Hash values can be omitted, meaning that value will be fetched from the context by the name of the key (source)
Hash literal value omission is a feature that allows you to omit the value of a hash key when the value is available in the current scope.
Email content
You will discover how hash literal value omission works (the scopes where it will try to find the values based on key name).
You can read code samples where hash literal value omission can simplify the code and make it easy to change. I showcase where using it might not make the code clearer, so it is best to avoid it. -
Pattern Matching
Pattern matching is a feature allowing deep matching of structured values: checking the structure and binding the matched parts to local variables. (source)
Pattern matching is a feature that allows you to match and destructure objects in a single expression. It comes with a standalone operator
=>
and acase/in
syntax.Email content
You will discover examples of using pattern matching implemented in the two ways that Ruby supports: using
case/in
and using the standalone operator<expression> => <pattern>
I will explore using=>
as the left-hand assignment operator andcase/in
to match and unpack objects to values conditionally.I will present cases where this can be useful when writing Rails code, using matching by object type, a combination of patterns using the
|
operator, and matching your own objects by implementingdestruct_keys
anddestruct
.This will be split over multiple emails, each focused on a use case, explaining the code and its benefits, while discussing readability, brevity, and verbosity.
-
Object Shapes
Object shapes are a technique for representing properties of an object. (…) Each shape represents a specific set of attributes (instance variables and other properties) and their values. In our implementation, all objects have a shape. The shapes are nodes in a tree data structure. Every edge in the tree represents an attribute transition (source)
Object Shapes is a performance optimization that uses a tree structure to improve how Ruby VM stores and looks up instance variables. YJIT and Ruby 3.3 use the object shapes to add more improvements and performance optimizations.
Email content
You will see examples of how to transform your code to take advantage of object shapes and minimize the number of different created shapes or object shape transitions while running your code.
It will have examples of how to declare instance variables and how to do memoization so you keep your code shape-friendly. -
What’s new in core classes and standard library
Between Ruby 2.7 and Ruby 3.3, there are a lot of changes to core classes or stdlib: new methods are added, other methods are improved, or there are changes in how they handle some cases.
Email content
I will cover
Range
,Set
,Filesystem
andIO
IO,Class
andModule
,String
,Hash
(andEnumerator
in general),Time
,Ractors
, and more.This topic is split into multiple emails, each explaining the change and showing code examples for using it. The focus is to show how to use these in production-like code.
Why take this course?
Why take this course? Here is a list of possible reasons!
Improve how you write your code by using new Ruby features
The first argument is that you want to discover this new syntax, look at code samples using it, and read my explanation about when and how to use it. See below for more details about each topic that I plan to cover.
Get time back by reading practical, focused content
The information I share in this course is not new - you can find a wide range of articles written by Ruby core committers and senior and junior developers about each topic. If you have time, you should read those articles/blog posts/Ruby tracker discussions/Reddit/Mastodon/X/threads. Getting information from people working on the features is always better.
If you don’t have time to do this, you can rely on my ability to curate and summarize content. I promise that I am reading all this information, and I can distill it and send you the most important parts - of course, written in my way, focusing on showing practical usage and code samples.
Like I do in my Short Ruby Newsletter and a matter essential to me, all sources will be adequately cited. So, if you want to expand a specific part of a topic, you can follow the list of cited resources and see more perspectives.
Real code samples
I will show you code samples that are commonly used in production Ruby or Ruby on Rails applications, rather than those only created for learning purposes. You will read before and after code samples and see how to transition from one code design to another.
Human content
Built by humans, not AI
The texts and content on this website are human-made and NOT generated using AI.
I am using Grammarly or similar software to fix grammar mistakes, which might rephrase some things, but I am not
generating the content with AI.