4 ms·
Convention over configuration: sane default values. magic: changing the scope in which a block is eval'd so you can inject BlankSlates that have method_missing
by aaronblohowiak 16y ago
Convention over configuration: sane default values.
magic: changing the scope in which a block is eval'd so you can inject BlankSlates that have method_missing defined on them that then do some string manipulation in order to pick which code path to take.
Sometimes you need to lean pretty hard on the super-dynamic features to get the api you want. But is it worth it?
- JonnieCache 16y agois this the routing DSL you're referring to here? The activerecord query building stuff is much simpler than this, i know that. If anyone wants to get a head start in understanding the magic, Ryan Bates has done excellent walkthroughs of the routing DSL and the activerecord query building method chaining source code: http://railscasts.com/episodes?utf8=%E2%9C%93&search=walkthrough http://railscasts.com/episodes?utf8=%E2%9C%93&search=wal... edit: to add to the list of stuff that rails does which is 'magical,' I still get uncomfortable over the way that instance variables I set in the controller scope are 'copied' to the view scope. This is obviously a necessary evil though. The real cost of rails' magic is the rampant and wanton Demeter Violation, which can make testing very difficult in some circumstances. http://en.wikipedia.org/wiki/Law_of_Demeter http://en.wikipedia.org/wiki/Law_of_Demeter
- alextgordon 16y ago"Ugh, I made this routing library, but the API is such a hack." "What about calling it a 'DSL' rather than an API? Then nobody will be able to claim you're abusing Ruby, because it's an entirely new language!" "I doubt anyone would ever buy that..."
- JonnieCache 16y agoIts called a DSL because it is extremely declarative, and because while it is obviously a subset of ruby syntax, it does not have to look like common ruby at all. I take your point though, I do get that feeling sometimes. There is a lot of showing off by ruby API developers. I think there comes a time in every ruby hackers life when they get the urge to write an API that makes use of every single metaprogramming/reflection/self-modification feature in the language, because it looks like so much fun. We spend our work days writing rails with all its fancypants DSL-like bits, and we think "I want to do one of them!" Mine is a DSL for defining regular expressions in natural language, and my god does it abuse metaprogramming. All the methods are defined at runtime by iterating over datastructures that list the regex spec. instance_eval(&block) all over the place. It is fucking brilliant. And not finished. Obviously.
- ollysb 16y agoThe point about the templates is that they're evaluated in the same context as the actions. Have a common context between the controller and the view makes a lot of sense to me. I don't see any reason why this is necessary though, you could always pass a context object to the template parser.