If yes: then it looks like you're proposing an alternative syntax for
Hash literals - one which isn't widely used. It's Ruby-inspired but not
really Ruby, since a simple eval of the above will fail without
additional supporting code.
That is, to parse that record using eval I think you need something
along these lines:
class Parser # < BasicObject ??
=A0 def self.parse(*args, &blk)
=A0 =A0 o =3D new
=A0 =A0 o.instance_eval(*args, &blk)
=A0 =A0 o.instance_variable_get
@value)
=A0 end
=A0 def initialize
=A0 =A0 @value =3D {}
=A0 end
=A0 def method_missing(label, value=3D:__MISSING__,&blk)
=A0 =A0 if value !=3D :__MISSING__
=A0 =A0 =A0 raise "Cannot provide both value and block for #{label}" if
block_given?
=A0 =A0 =A0 @value[label] =3D value
=A0 =A0 else
=A0 =A0 =A0 raise "Must provide value or block for #{label}" unless
block_given?
=A0 =A0 =A0 @value[label] =3D self.class.parse(&blk)
=A0 =A0 end
=A0 end
end
person =3D Parser.parse <<'EOS'
=A0 name "Joe Foo"
=A0 age 33
=A0 contact do
=A0 =A0 email "(e-mail address removed)"
=A0 =A0 phione "555-555-1234"
=A0 end
EOS
p person
Now, to get the same capabilities as JSON, you also need syntax for
Arrays. You could do something like the following, although note that
the parser above doesn't handle this properly:
=A0 people([
=A0 =A0 person do
=A0 =A0 =A0 name "Joe"
=A0 =A0 =A0 age 33
=A0 =A0 end,
=A0 =A0 person do
=A0 =A0 =A0 name "Fred"
=A0 =A0 =A0 age 64
=A0 =A0 end,
=A0 ])
You do need both parentheses and square brackets, unless you replace
do/end with braces:
=A0 people [
=A0 =A0 person {
=A0 =A0 =A0 name "Joe"
=A0 =A0 =A0 age 33
=A0 =A0 },
=A0 =A0 person {
=A0 =A0 =A0 name "Fred"
=A0 =A0 =A0 age 64
=A0 =A0 },
=A0 ]
Add a few colons and commas and you're back to JSON. Or drop the closing
braces and brackets and keep the indentation, and you're back to YAML.
You could treat multiple arguments as an array:
=A0 people \
=A0 =A0 person {
=A0 =A0 =A0 name "Joe"
=A0 =A0 =A0 age 33
=A0 =A0 },
=A0 =A0 person {
=A0 =A0 =A0 name "Fred"
=A0 =A0 =A0 age 64
=A0 =A0 }
but then if you wanted a one-element array it would have to be a special
case. Or you could perhaps have a flag to indicate that you want an
Array:
=A0 people [],
=A0 =A0 person {
=A0 =A0 =A0 name "Joe"
=A0 =A0 =A0 age 33
=A0 =A0 },
=A0 =A0 person {
=A0 =A0 =A0 name "Fred"
=A0 =A0 =A0 age 64
=A0 =A0 }