If you are not in Bangalore find someone who knows and ask him to teach you. :-)
[Rant] These things have been true from the dawn of programming languages
- A language with a built in eval function allows you to transform code into a parse tree at runtime.
- To the degree that the language has a uniform/flexible syntax, this transition between code and data is painless
- All language transforms consist of two steps . First you convert a string into a data structure called a "syntax tree" . This is called parsing. Then various transformations can be done on this data structure to produce the effect you want.
Thus the string "2 + 3" can be converted into a datastructure like "{ operator: "+", operand1:"2", operand2:"3"}".
Then you can define various transforms that work on (or "visit" in oo terms) the components of the data structure and do whatever you want.
e.g : (pseudocode) def print(anAst) puts anAst.operand1,anAst.operator,anAst.operand2 end
def printReversePolish(anAst) puts anAst.operand1,anAst.operand2,anAst.operator end
def evaluate(anAst) return anAst.operand1 + anAst.operand2 end
anAst = parse("2 + 3")
print(anAst) => 2 + 3
printReversePolish(anAst) => 2 3 +
evaluate(anAST) => 5
and that is all there is (conceptually) to any langauge transform.
- Ruby "DSL" s allow people who haven't studied the fundamentals of computer science to think they are discovering something new and amazing. Yes, Ruby allows a somewhat seamless mixing of "pure" ruby and "dsl" code, but this is just as true for any langauge with a built in eval and clean syntax. (lisp, forth, even smalltalk). The built in "eval" takes care of the parsing (as long as the syntax does not vary too widely from ruby syntax)
To understand ruby "dsl" s, ignore all the hype (and the forthcoming stream of books explaining how ruby's 'dsl ability' is the next silver bullet that will save enterprise programmers , give their arid lives meaning, and solve world hunger), understand how interpreters work (read "The Essentials Of programming Languages") and meditate on these concepts in ruby - class eval, object_eval, method_missing. if you understand the basics of how languages work, ruby's "dsl" is some (very) old wine in some sparkling new bottles.
Ok , biting your tongue and walking away quietly is almost as good.
DSLs like any other programming technique, when used appropriately gives some beneficial effects. But it is no panacea and a casual perusal of the net will reveal a bunch of atrocious dsls . Beware the hype and snake oil. [end Rant]