Feature #5578
closedEmbedded YAML for Ruby 2.0
Added by trans (Thomas Sawyer) about 13 years ago. Updated about 13 years ago.
Description
Way cool would be support for embedded YAML.
data = ---
a: 1
b: 2
c: 3
...
Files
noname (500 Bytes) noname | Anonymous, 11/09/2011 01:23 AM | ||
prelude.rb.yaml.patch (277 Bytes) prelude.rb.yaml.patch | technohippy (Yasushi ANDO), 11/12/2011 03:54 PM | ||
parse.y.yaml.patch (4.1 KB) parse.y.yaml.patch | technohippy (Yasushi ANDO), 11/12/2011 03:54 PM |
Updated by kernigh (George Koehler) about 13 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
Updated by trans (Thomas Sawyer) about 13 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'=>#<Proc ...>, 'c'=>2}
You get the idea.
Updated by trans (Thomas Sawyer) about 13 years ago
Hey, btw, how do you get
section on this thing?
Updated by kstephens (Kurt Stephens) about 13 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.
Updated by judofyr (Magnus Holm) about 13 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); endx = 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
Updated by Anonymous about 13 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); endx = 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); endx = YAML <<"..."
a: 1
b: 2
...
p x
I will add this.
--
Aaron Patterson
http://tenderlovemaking.com/
Updated by trans (Thomas Sawyer) about 13 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.
Updated by drbrain (Eric Hodel) about 13 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
Updated by rkh (Konstantin Haase) about 13 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
Updated by now (Nikolai Weibull) about 13 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?”.
Updated by alexeymuranov (Alexey Muranov) about 13 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.
Updated by angdraug (Dmitry Borodaenko) about 13 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?
Updated by technohippy (Yasushi ANDO) about 13 years ago
- File prelude.rb.yaml.patch prelude.rb.yaml.patch added
- File parse.y.yaml.patch parse.y.yaml.patch added
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.
Updated by nobu (Nobuyoshi Nakada) about 13 years ago
Does YAML end by "..." in middle of a line?
Updated by trans (Thomas Sawyer) about 13 years ago
Does YAML end by "..." in middle of a line?
No. It only ends when flush left.
Updated by tenderlovemaking (Aaron Patterson) about 13 years ago
- Status changed from Open to Rejected