6 ms·
Blocks are also an extremely powerful tool when it comes to building DSLs, and if done correctly are a great alternative to complicated options files/hashes/etc
by subwindow 17y ago
Blocks are also an extremely powerful tool when it comes to building DSLs, and if done correctly are a great alternative to complicated options files/hashes/etc. Rails' routes and config/intializer come into mind.
- chromatic 17y ago> Blocks are also an extremely powerful tool when it comes to building DSLs... I can't read this as anything more insightful than functions are also an extremely powerful tool when it comes to building APIs. I first used Ruby in 2000. What am I missing?
- jamesbritt 17y ago"What am I missing?" Nothing. The emphasis on DSLs is distracting. Worse, it may lead people to think that certain coding techniques are only used when creating a "DSL".
- Vitaly 17y agoruby block syntax is clean, elegant and nest-able which allows creating good looking DSLs. foo do bar do .. end baz 123 end now try to do the same with some other language which supports something-kind-of-like-ruby-blocks but with a different syntax. it will not look nearly as good, so in those languages instead of creating DSL people usually implement some kind of config file format instead. or just use XML :) if you'd have to use 'lambda' to define a block for example, it would make a much worse DSL with lots of extra syntax noise.
- chromatic 17y agoThe lack of punctuation characters makes this a DSL and not bog-standard Ruby code? > ... instead of creating DSL people usually implement some kind of config file format instead. or just use XML. Writing a grammar or a parser means you haven't created a DSL? Unless by "DSL" you mean "Ruby syntax and Ruby semantics with symbol names chosen by the programmer", I have no idea what you mean by "DSL".
- jimbokun 17y agoHow about this: Ruby's syntactic flexibility and semantic model mean that many cases handled by a change to the language spec or delegating to another language (XML config files, for example) can be cleanly handled in Ruby itself. The end result is often something that looks like a little language for a specific task, such as Rake, Rails, etc. (I'm not a Ruby programmer, so I hope those are good examples.) So with Ruby, there are very few cases where writing a custom grammar or parser is necessary. Ruby's flexibility gives you the ability to do things that might require a custom grammar or parser in other languages.
- draegtun 17y agoHowever even Jim Weirich doesn't like Rake being called a "DSL". I think Piers Cawley was dead on by calling things like this a Pidgin refs: * http://www.infoq.com/interviews/jim-weirich-discusses-rake http://www.infoq.com/interviews/jim-weirich-discusses-rake * http://www.bofh.org.uk/2007/08/08/domain-specific-pidgin http://www.bofh.org.uk/2007/08/08/domain-specific-pidgin
- flogic 17y agoCommon usage of DSL seems to mean an API whose usage looks more like a secondary language rather than the host language. At least that's my interpretation. A case example would be jQuery. Which while it is JavaScript, idiomatic code using jQuery isn't written in the same style as traditional JavaScript.