DHH · 2025-07-12 · transcript-lex-474-2025 · source ↗
Metaprogramming
`1:03:34`
You get to add keywords to the language. Active record is the part of Rails that communicates with the database. This is a system where every table in the database is represented by a class. So if we take the user example, again, you do class user descends from active record base, and then the first line you can write is this. I want my users to have many posts or have many comments. Let's do that. We're making some system where users can make comments. The very next line is has_many, space, colon comments. Now you've set up a dependency between users and comments that will give your whole host of access and factory methods for users to be able to own comments, to create comments, to update comments in that line alone has many looks like a keyword. It looks like it's part of the Ruby language.
`1:04:35`
That's metaprogramming. When Rails is able to add these elements to how you define a class and then that runs code that adds a bunch of methods to the use of class, that's metaprogramming. And when metaprogramming is used in this way, we call it domain specific languages. You take a generic language like Ruby and you tailor it to a certain domain like describing relationships in a database at a object level. And this is one of those early examples where you can do, user has many comments. Belongs _two_:account. Now you've set up a one-to-one relationship before we had a one-to-many relationship. Rails is rife with all these kinds of domain specific languages where at sometimes it doesn't even look like Ruby. You can't identify Ruby keywords. You can just identify what looks like keywords in its own programming language.
`1:05:37`
Now again, I know that Lisp and others also do this stuff. They just do it with the maximum amount of line noise that can ever be crammed into a programming language. And Ruby does it at a level where you cannot tell my metaprogramming from Matz's keywords and with zero line noise. — Yeah, I should say that my first love was Lisp. So there's a slow tear that you can't see. — I've actually never written any real Lisp myself. — Well, how can you judge it so harshly then? — 'Cause I have two eyes and I can look at code, and my aesthetic sensibilities forbid me to even go much further. Which is a limitation, I know. I should actually dive into Lisp because I've found that I've learned a lot just diving into, maybe I'm insulting Lisp again here, but the past of programming languages with Smalltalk, for example. I think Smalltalk is a incredible experiment that also worked but isn't suitable for today's programming environments. — I love that we're talking about Ruby