[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Inline YAML in Ruby code - possible? converse?

Its Me

10/26/2004 4:07:00 PM

I recall an old post by Why which described some way to inline chunks of
YAML right into Ruby code. Could not locate it in Google. Roughly like

def foo
a = %y
r: abc
s: pqr
t: - 1
- 2
- 3
%y
return a
end

Does anyone have the details, and what to include to enable this processing?

Conversely, is there any way to include Ruby code inline in YAML? Could "!!"
be faked out to accomplish this?

And lastly, could YAML be used in a re-entrant way e.g. could the code
processing a private type !! in the middle of a YAML::load in turn make a
nested call to YAML::load?

Thanks!


6 Answers

gabriele renzi

10/26/2004 4:34:00 PM

0

Its Me ha scritto:

> I recall an old post by Why which described some way to inline chunks of
> YAML right into Ruby code. Could not locate it in Google. Roughly like
>
> def foo
> a = %y
> r: abc
> s: pqr
> t: - 1
> - 2
> - 3
> %y
> return a
> end
>
> Does anyone have the details, and what to include to enable this processing?

you have to patch ruby and recompile

> Conversely, is there any way to include Ruby code inline in YAML? Could "!!"
> be faked out to accomplish this?
>
> And lastly, could YAML be used in a re-entrant way e.g. could the code
> processing a private type !! in the middle of a YAML::load in turn make a
> nested call to YAML::load?

dunno, sorry :/

T. Onoma

10/26/2004 4:45:00 PM

0

On Tuesday 26 October 2004 12:34 pm, gabriele renzi wrote:
| Its Me ha scritto:
| > I recall an old post by Why which described some way to inline chunks of
| > YAML right into Ruby code. Could not locate it in Google. Roughly like
| >
| > def foo
| > a = %y
| > r: abc
| > s: pqr
| > t: - 1
| > - 2
| > - 3
| > %y
| > return a
| > end
| >
| > Does anyone have the details, and what to include to enable this
| > processing?
|
| you have to patch ruby and recompile

You can use Peter Vanbroekhoven's patch for user defined %-methods. Find it
here:

http://developer.berlios.de/proj...

%y for YAML is the first example of it's use:

a = %y{
r: abc
s: pqr
t: - 1
- 2
- 3
}


T.


T. Onoma

10/26/2004 4:56:00 PM

0

On Tuesday 26 October 2004 12:09 pm, Its Me wrote:
| Conversely, is there any way to include Ruby code inline in YAML? Could
| "!!" be faked out to accomplish this?
|
| And lastly, could YAML be used in a re-entrant way e.g. could the code
| processing a private type !! in the middle of a YAML::load in turn make a
| nested call to YAML::load?
|
| Thanks!

On a related note, I would like to see Ruby conform to YAML multipart document
system. Rough idea:


--- !!rubyscript

# blah blah blah

--- !!rubyscript/test

# tests

--- !!rubyscript/data

# instead of __DATA__


I'm sure this can be improved upon / thoughout more too.

T.


Florian Gross

10/26/2004 7:26:00 PM

0

gabriele renzi wrote:

> Its Me ha scritto:
>
>> I recall an old post by Why which described some way to inline chunks of
>> YAML right into Ruby code. Could not locate it in Google. Roughly like
>>
>> def foo
>> a = %y
>> r: abc
>> s: pqr
>> t: - 1
>> - 2
>> - 3
>> %y
>> return a
>> end
>>
>> Does anyone have the details, and what to include to enable this
>> processing?
>
> you have to patch ruby and recompile

You can also use the source code filter technique described in
[ruby-talk:116822], but you would need to implement the syntax for
yourself. The benefit of this is that it will not need a recompile of Ruby.

Peter

10/26/2004 8:55:00 PM

0

T. Onoma

10/27/2004 12:12:00 AM

0

On Tuesday 26 October 2004 04:54 pm, Peter wrote:
| > You can use Peter Vanbroekhoven's patch for user defined %-methods. Find
| > it here:
| >
| > http://developer.berlios.de/proj...
|
| I just wanted to add that if you find the courage to patch the Ruby source
| and recompile it, be aware that the patch is not backward compatible with
| current Ruby; it breaks a number of things, and changes some other things.

Hmm.. that sort of makes it sound like it will bring down your whole system :)
I don't think its that bad. I've tried it and works rather well. Here are the
specific notes below. Is there more to it than these points? If no, then I
think it's pretty usable. Just be aware of following potential snags.

Notes: Patches the ruby source (v1.8.2, may work for other versions) such that
it allows to redefine the %-literals. The literals are enabled when passing
-% as option to the Ruby interpreter. This is done to enable Ruby to still
compile its extensions, since there are a number of backwards
incompatibilities incurred by this patch:

* %x becomes %X (%x still works but without escaping)
* %r becomes %R (ditto)
* The o option for %r is broken.
* %W is _terminally_ broken. Try %W{ hello\ world #{} }
and see what happens before & after.
* 'def %a' no longer means 'def %(a)'
* Cannot be called with a receiver

Nontheless, it is an expiremental patch. Do not use in production
environments!

| If you have some patience, I'll brew a version that can retain backward
| compatibility at the price of lack of uniformity.

I'd prefer the uniformity. I don't think %r and %x, nor def %a, are commonly
used enough to fuss about. /%r/%R/ is easy enough any way.

T.