Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Ruby on Rails called a domain-specific language?

Recently, when I tried to explain why Ruby on Rails is a domain-specific language (DSL) to an intern at my organisation, I was not able to articulate my reasoning to the effect I would like to. Maybe I do not understand the space well enough to teach the nuances. Redirecting him to Martin Fowler' article or the Google ranked one InfoQ or other material has not helped much either.

Can some explain why Rails is a DSL with an example / parallel situation which is not voodoo stuff for someone who is fairly new to the world of code? Understanding the ideology might also help in elaborating the intricacies of the rails ecosystem?

like image 785
paddle42380 Avatar asked May 12 '10 19:05

paddle42380


1 Answers

Technically, ruby is not a DSL, it just lends itself to writing internal DSLs very cleanly. This link to Martin's Fowler's blog wiki should help clarify things.

Rails has been described as a DSL although I think of it more precisely as a framework that makes very good use of a few DSLs.

Edit: The intro to the public version of Martin Fowler's DSL book has a motivating example, although it is still aimed a bit more at programmers.

Edit again: The "voodoo" example can be useful if you point out that ruby allows ruby code to look like

port 2001

respond :resource=>"/hello" do  |request, response|
  response.body = "<message>hello</message>"
end

which is cleaner than calling the methods directly. Implementing a DSL can require "voodoo" like code (it is much harder creating a DSL than using one), and in general someone relatively new to coding should worry first about what the DSL does and not so much how it is implemented.

What makes ruby good at internal DSLs includes (1) use of blocks (any language with clean closures has this advantage), (2) method class that do not require parentheses, and (3) the ability to modify classes on the fly (which is what validates does). There are probably more I haven't thought of.

like image 170
Kathy Van Stone Avatar answered Oct 22 '22 15:10

Kathy Van Stone