Metaprogramming inside: am I method_missing something?, by Vladimir Dementyev

Abstract

Ruby was designed to make programmers happy. One of the features that makes it possible is a comprehensive metaprogramming tooling.

What is metaprogramming? How is it implemented in Ruby?

Let's take a look under the hood of the simplest meta-technique–method_missing–and discover what lies behind it.

And to make this investigation more meta-exciting we'll try to re-write it from scratch but in Ruby! So, don't be afraid of C code)

Details

Let's discuss first what metaprogramming is:

  • by example: from well-known DSLs to crazy syntax hacking. Metaprogramming is all around us.
  • by definition: "Metaprogramming is ..."
  • by structure ("What does metaprogramming consist of?"): code generation, syntax extensions, introspection/reflection (the latter one, of course, is not meta itself but heavily used for).

A quick overlook of meta tooling we have in Ruby (open classes, dynamic dispatch, etc.).

The main part of the talk–method_missing.

In my opinion, method_missing is the first meta-technique which every Ruby developer got familiar with.
But it turned out to be not that simple. Let's dig into it!

Our first stop is dynamic dispatch and how it's implemented in Ruby. A quick overview of Ruby object model.

Question #1: When do we get to method_missing? The answer could surprise you (a little bit). That's just a warmup.

Question #2: Is method_missing slow? Spoiler: it depends.

That's where the most interesting part of talk starts: we found something interesting–method_missing behaves differently in different situations. Why so?

Let's try to read through all the C source code to find the truth. That's a joke. C code just won't fit the slides)

I know a better way – let's implement our own dynamic dispatch in pure Ruby! (By the way, using different metaprogramming techniques along the way).

There gonna be three (or four) iterations: form the naïve one, without any optimization, to the high-level one with caching techniques and monkey-patching awareness.

That's how I'd like to demonstrate that Ruby is an excellent piece of software, with brilliant hidden gems inside.

As a bonus (=more practical) part of the talk I'd like to demonstrate one meta-technique related to the main topic – lazy method missing.

The primary goal of this talk to shed light on the internals of some Ruby features, to make it easy to understand, how Ruby VM works, and share a handful of practical tips.

Explaining Ruby core through pure Ruby makes this talk relevant to Rubyists of any level!

Pitch

This talk covers two topics: Ruby metaprogramming and Ruby internals.

The first one is an everyday tool and the second one is a forbidden forest for most Ruby developers.

Learning Ruby internals is hard: it's a 25 years C codebase with dozens of 1000+ LOC files! What makes us happy requires a lot of effort and doesn't look attractive at all.

On the other hand, understanding how things work could help you to become a better developer.

The goal of this talk is to make the gap between the Ruby source C code and the "happy" Ruby code as small as possible.

Learn Ruby through Ruby!–the motto of my talk.

Edit proposal