David Heinemeier Hansson (DHH)
๐ค SpeakerAppearances Over Time
Podcast Appearances
So that takes all of these elements we've just talked about and turned them up to 11.
I'll explain metaprogramming real simple.
Metaprogramming is essentially a version of the 5.days.
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 underscore many space
colon comments.
Now you've set up a dependency between users and comments that will give you a 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.
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.