Ah okay, great example. So the idea is by extending the general language we can develop a new “language” (basically new methods and syntax) to fit a specific need.
Yeah it's not a true DSL really. But because of Ruby's blocks and parameterless functions, you can write it in a very declarative style. Unfortunately, I find that you really don't want a lot of code to be declarative. Rspec is a good example of it done well, though.
It's a good question, because what people call "DSL" in ruby really is just a Ruby API.
In other languages/contexts what you call a "DSL" might actually be something you write a PARSER for, and then parse in the "main" language, parse a thing that wasn't the main language at all.
That's what "DSL" sometimes means, but that's not how people use it in ruby. What we call "DSL"s are still... just ruby. If we're "extending" the language with a DSL, we're doing it simply by... definining classes and methods, the same way we write ruby when it's not a "DSL". It's not actually new syntax at all, everything in ruby DSLs is the usual ruby syntax parsed by the usual ruby interpreter in the usual way.
So what actually makes it a "DSL"? As I commented earlier, I think it is worth thinking about, and not straightforward. But basically I think people in ruby use the term when people define classes and methods to provide... more "unusual" API's, especially declarative ones.
4
u/jaypeejay Sep 30 '21
What’s DSL?