From: Intransition Date: 2010-09-25T16:49:14+09:00 Subject: Re: Ruby-based data language On Sep 22, 3:17 pm, Brian Candler wrote: > Thomas Sawyer wrote: > > On Sep 22, 7:20 am, Brian Candler wrote: > >> I'd have said most packages use YAML. If you can provide examples which > >> use method_missing and block-based structure just for conveying static > >> configuration information, I'd be interested to see them. But then > >> that's what you asked for in the first place :-) > > > I think a Gemfile is a pretty good example > > Do you mean a Gemfile from Bundler? e.g. > >   source "http://rubygems.org" > >   gem "rails", "3.0.0.rc" >   gem "rack-cache" >   gem "nokogiri", "~> 1.4.2" > > Some attributes have one value, some attributes have more than one > value, some attributes can be repeated. I guess a generic parser API for > that would be a call to callback(key, *args), just like method_missing, > or it could build a linear data structure like > >   [[:source,"http://rubygems.org"], >    [:gem,"rack-cache"], >    [:gem,"nokogiri","~> 1.4.2"]] > > I'm not sure what output you'd want from nested blocks. > > > I would expect to get an object that gave me access to the data. What > > kind of object depends on the parser. > > If the parser is generic (not application-specific) then I guess you'd > just get the structure I've shown above. That's one way. The parser could just offer a couple of options to vary how the generic structure is formed. > > I get what you are saying. But even JSON goes through a parser in > > Javascript > > It *can* go through a parser for security reasons, but the result from > parsing it is exactly the same as if you read it in as a Javascript > literal. And there are cases where you intentionally interpret it as > Javascript code, e.g. JSONP. > > > when someone thinks "Ruby- > > syntax data/config format" they are thinking builder-style, not hash > > literals. > > Perhaps. I think more common would be a Rails-style configuration or a > gemspec: > > Gem::Specification.new do |s| >   s.name = %q{snailgun} >   s.version = "1.0.6" >   ... etc > end That's another way to do it, yes. But can it be made a pure data format? > Of course, the advantage of having it as real executable code is that > you can use Ruby to assemble data constructs, and conditional inclusion. > >   s.files = [ ...lots of items... ] >   s.files.concat [ ...more items... ] > >   if s.respond_to? :specification_version then >     s.specification_version = 2 >   end But there's the disadvantage here too --it's no longer just data. Depends on what you're trying to achieve.