Thomas S. wrote:
On Sep 22, 7:20�am, Brian C. [email protected] 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. If you want to build that into
application-specific objects, then you can do so. For example, for each
“gem” line you might want to build a Gem object and append it to an
Array of gems.
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
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