# Ruby Don't E4X

**URL:** <https://www.ruby-forum.com/t/ruby-dont-e4x/94102>\
**Category:** Ruby\
**Created:** [April 20, 2006, 9:55pm UTC](https://www.ruby-forum.com/t/ruby-dont-e4x/94102 "2006-04-20T21:55:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 20, 2006, 9:55pm UTC](https://www.ruby-forum.com/t/ruby-dont-e4x/94102/1 "2006-04-20T21:55:49Z")

</div>

I began work on an implementation of E4X for Ruby. I decided to tackle  
the problem from two ends. I began with a literal translation of the  
Javascript-esque pseudo-code given in the specification in order to  
obtain a fully compliant implementation. That will unfortunately take  
a long while to complete. But I wanted something I could use  
post-haste, so I also created a fresh-thought “lite” version, based on  
[  
Jack Herrington’s ONLamp.com article](http://www.onlamp.com/pub/a/onlamp/2004/08/12/ruby_e4x.html), but now much expanded. I  
figure in the course of time, the two versions will meet in the middle  
for a very Ruby-esque delivery of E4X.

Well, I like to dream. Truth is Ruby just can’t provide suitable  
syntax without revision to core. Every alternative I’ve tried is worse  
than not using E4X in the first place. First there’s the issue of name  
clash with built-in methods and the tag names. Does ‘x.class’ access  
element node \<class\>…\</class\> or the class Xml? I tried  
some variable solutions and settled on a bit of delegation. To get at  
these built-ins one first calls a public #self method.

x.self.class

That’s acceptable. We’ll manage. But then there’s the worse problem of  
attributes access. Let’s say we have this XML:

x = %{ft:colorgreen}

Standard E4X attribute access notation is:

x.@wartindex #=\> 10

The '@' won't fly. Too bad. What alternative is there? Jack Herrington offers:

x.\_wartindex

Well, ick. I mean, it’s livable, but underscore methods generally  
represent hidden methods, not something to be commonly used.

Then there’s ugly problem of Namespaces. The whole call notation  
starts to get really shakey since you can’t do:

x.ft:color

I could try to trick it, making ‘ft’ act like a method passing in the  
:color tag. That would be weak though. Parenthesis would come into  
play when assigning or calling additional methods againt it.

x.ft(:color)

Would that make sense? Getting real iffy now. In fact, at this point I  
figure we’re one step away from saying screw this and using hashes for  
all of it.

x[‘@wartindex’]  
x[‘ft:color’]

Look real nice, even if a little more verbose. And then we can dump  
the x.self.class thing too. But then, it’s not so much E4X anymore. Is  
it?

What do you think? Is E4X important enough to change Ruby? Keep in  
mind that full compliance would require some changes anyway since  
notions like the following are part of the standard:

x.@\*

x = ft:colorgreen

Notice there are no “quotation” brackets.

T.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 20, 2006, 10:14pm UTC](https://www.ruby-forum.com/t/ruby-dont-e4x/94102/2 "2006-04-20T22:14:14Z")

</div>

TRANS wrote:

> What do you think? Is E4X important enough to change Ruby?

No. Ruby isn’t ECMAScript. Trying to make it look exactly like  
ECMAScript  
isn’t going to work. And, frankly, ECMAScript looks to me like nothing  
more  
than syntactic xpath flavored sugar. Hardly in the “important”  
category.

For what it’s worth, I think you’re on the right track with your own bit  
of  
syntax near the end of your post. A sort of DSL, perhaps?

Regards,

Dan
