Feature #5578

Embedded YAML for Ruby 2.0

Added by Thomas Sawyer over 2 years ago. Updated over 2 years ago.

[ruby-core:40770]
Status:Rejected
Priority:Normal
Assignee:-
Category:-
Target version:2.0.0

Description

Way cool would be support for embedded YAML.

data = ---
a: 1
b: 2
c: 3
...

noname (500 Bytes) Anonymous, 11/09/2011 01:23 AM

prelude.rb.yaml.patch Magnifier (277 Bytes) Yasushi ANDO, 11/12/2011 03:54 PM

parse.y.yaml.patch Magnifier (4.1 KB) Yasushi ANDO, 11/12/2011 03:54 PM

History

#1 Updated by George Koehler over 2 years ago

=begin
Current Ruby can do

require 'yaml'

data = YAML.load <<EOF
a: 1
b: 2
c: 3
EOF

p data

Suppose that Ruby adds embedded YAML, and I change my program to

data = ---
a: 1
b: 2
c: 3

p data

Where does YAML end? Is "p data" part of YAML, and not Ruby code? Is it decided by indentation?

=end

#2 Updated by Thomas Sawyer over 2 years ago

YAML defines ... as end of document marker.

data = ---
a: 1
b: 2
c: 3
... <= this

Yes, it is something like HERE docs, but more concise. I also have this gut feeling that it could evolve into in even more useful ways in the future, though I can't quite put my finger on it yet. Some interesting "hints" though include:

data = eval(source)

Where source could be Ruby or YAML.

load 'foo.yml'

You'd have to use a callback of some sort to get the data, but it's interesting nonetheless that it would work.

And I wonder if a "Ruby code escape" could be worked-in, e.g.

class A; end
foo = "foo"

data = ---
a: ->{ A }
b: ->{ ->{ foo } }
c: ->{ 1 + 1 }
...

data #=> {'a'=>A, 'b'=>#, 'c'=>2}

You get the idea.

#3 Updated by Thomas Sawyer over 2 years ago

Hey, btw, how do you get section on this thing?

#4 Updated by Kurt Stephens over 2 years ago

Why does YAML need first class status? Define a constructor on the YAML module, and use interpolating heredoc.

require 'pp'
require 'yaml'
def YAML.[]str; YAML.load(str); end

x = YAML[<<"..."]
a: 1
b: 2
c: #{ 1 + 1 }
...

pp x

And it's generic enough for other encodings:

xml = XML[<<"end"]
text
end

etc.

#5 Updated by Magnus Holm over 2 years ago

On Mon, Nov 7, 2011 at 16:30, Kurt Stephens ks.ruby@kurtstephens.com wrote:

Why does YAML need first class status?  Define a constructor on the YAML module, and use interpolating heredoc.

require 'pp'
require 'yaml'
def YAML.[]str; YAML.load(str); end

x = YAML[<<"..."]
a: 1
b: 2
c: #{ 1 + 1 }
...

pp x

Or you can simply define a method called YAML:

require 'yaml'
def YAML(str); YAML.load(str); end

x = YAML <<"..."
a: 1
b: 2
...
p x

#6 Updated by Anonymous over 2 years ago

On Tue, Nov 08, 2011 at 04:14:02AM +0900, Magnus Holm wrote:

On Mon, Nov 7, 2011 at 16:30, Kurt Stephens ks.ruby@kurtstephens.com wrote:

Why does YAML need first class status?  Define a constructor on the YAML module, and use interpolating heredoc.

require 'pp'
require 'yaml'
def YAML.[]str; YAML.load(str); end

x = YAML[<<"..."]
a: 1
b: 2
c: #{ 1 + 1 }
...

pp x

Or you can simply define a method called YAML:

require 'yaml'
def YAML(str); YAML.load(str); end

x = YAML <<"..."
a: 1
b: 2
...
p x

I will add this.

--
Aaron Patterson
http://tenderlovemaking.com/

#7 Updated by Thomas Sawyer over 2 years ago

@kurt "Why does YAML need first class status?"

On the surface it's just more elegant. e.g.

x = ---
    a: 1
    b: 2
    ...

vs.

x = YAML %{
      a: 1
      b: 2
    }

But see my last post about the potential for deeper integration. In short, the later can only ever handle a string. Integrating YAML more deeply could allow for Ruby objects to be inserted directly into the YAML structure.

---
time: ->{ Time.now }
...

Would be equivalent too

{
  'time' => Time.now
}

Why would you want to do that? For the very reasons we like YAML. It is an easy to read and concise format, especially when the data structure gets a little bit more nested than just a couple of elements (unlike the examples above).

Note also that JSON is a subset of YAML. So if Ruby supported YAML natively, it would also support JSON natively --just like Javascript.

#8 Updated by Eric Hodel over 2 years ago

=begin
This is valid ruby code:

x = ---
a

This is valid YAML:

p YAML.load "---\na: -> { Time.now }"
# => {"a"=>"-> { Time.now }"}

You seem to be proposing a new thing which collides with valid Ruby code and valid YAML. How do you propose to solve these problems? Can you show a concrete specification?

I think it will be easier to read and write YAML as a String where there is no ambiguity about the rules.

=end

#9 Updated by Konstantin Haase over 2 years ago

On Nov 8, 2011, at 15:29 , Thomas Sawyer wrote:

Issue #5578 has been updated by Thomas Sawyer.

@kurt "Why does YAML need first class status?"

On the surface it's just more elegant. e.g.

x

#10 Updated by Nikolai Weibull over 2 years ago

On Tue, Nov 8, 2011 at 19:29, Thomas Sawyer transfire@gmail.com wrote:

@kurt "Why does YAML need first class status?"

But see my last post about the potential for deeper integration. In short, the later can only ever handle a string. Integrating YAML more deeply could allow for Ruby objects to be inserted directly into the YAML structure.

Why is deeper integration necessary? What is it about YAML that’s
solves your problems? Whenever I’ve come across it, it makes me ask
“why aren’t you using Ruby instead?”.

#11 Updated by Alexey Muranov over 2 years ago

I think that when two different languages are used in a same source file, especially a whitespace sensitive one and a non whitespace sensitive, it is good to have them clearly separated.

#12 Updated by Dmitry Borodaenko over 2 years ago

Count me in on the "Nay" side: I strongly agree with Nikolai's and Alexey's points. Ruby as a language is already quote cool, and certainly cool enough for me. Ruby as a platform, on the other hand, has lots of bugs. Can we please do more of fixing bugs and less of adding new cool stuff, at least for a while?

#13 Updated by Yasushi ANDO over 2 years ago

Hi Thomas, this is a stunning idea for me! I attached my patches of bad quality to add the yaml literal to ruby1.9.3. I wish they could help you.

#14 Updated by Nobuyoshi Nakada over 2 years ago

Does YAML end by "..." in middle of a line?

#15 Updated by Thomas Sawyer over 2 years ago

Does YAML end by "..." in middle of a line?

No. It only ends when flush left.

#16 Updated by Aaron Patterson over 2 years ago

  • Status changed from Open to Rejected

Also available in: Atom PDF