On Sep 19, 5:38=A0am, Brian Candler <b.cand... / pobox.com> wrote:
> Thomas Sawyer wrote:
> > Has anyone ever endeavored to create a data/configuration file format
> > based on Ruby's syntax? In other words, a format like YAML but with a
> > Ruby-based syntax.
>
> Which bit of Ruby syntax are you thinking of?
>
> If you wanted key/value pairs in a Hash, then you might as well use
> JSON. It's not pretty for config files, but it's OK.
>
> If you wanted
>
> =A0 =A0 foo =3D 123
>
> then that's hard to make work, although maybe you could frig it with
> binding and local_variables. You could instead use the Rails way:
>
> =A0 =A0 config.foo =3D 123
>
> or use constants:
>
> =A0 =A0 module Config
> =A0 =A0 =A0 Foo =3D 123
> =A0 =A0 end
>
> or globals:
>
> =A0 =A0 $foo =3D 123
>
> The Sinatra way would be:
>
> =A0 =A0 set :foo, 123
> =A0 =A0 set(:bar) { delayed_expr }
>
> Maybe what you're trying to do is
>
> =A0 =A0 foo 123

Yes, I should have been more specific. This is what I mean. A longer
example:

  name "Joe Foo"
  age 33
  contact do
    email "joe / joefoo.com"
    phione "555-555-1234"
  end

> I don't see a particular problem with that, even with $SAFE=3D4 and
> method_missing:
>
> >> $h =3D {}
> =3D> {}
> >> def method_missing(k,v); $h[k] =3D v; end
> =3D> nil
> >> t =3D Thread.new { $SAFE=3D4; eval "foo 123" }
>
> =3D> #<Thread:0x7f4eb7749a48 dead>>> t.join
>
> SecurityError: (irb):2:in `[]=3D': Insecure: can't modify hash
> =A0 from (irb):3
> =A0 from (irb):4:in `join'
> =A0 from (irb):4
> =A0 from :0>> $h.taint
> =3D> {}
> >> t =3D Thread.new { $SAFE=3D4; eval "foo 123" }
>
> =3D> #<Thread:0x7f4eb7726930 dead>>> t.join
>
> =3D> #<Thread:0x7f4eb7726930 dead>>> $h
>
> =3D> {:foo=3D>123}
>
> This is using ruby 1.8.7 (2010-01-10 patchlevel 249) [x86_64-linux]

Ah. You are right. I made a mistake in my experiments. And thank you
for this example, #taint made all the difference.

So now the question: is $SAFE=3D4 safe enough? My gut says no. Even
though the above works, I think ultimately a true data format is
desirable for most needs, such as configuration files. I think Ruby
has a very nice syntax for it. Such a format would be to Ruby as JSON
is to Javascript. And Ruby has a nice advantage with blocks.