Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #4042

Re: newbie to using builder(for xml markup)

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!talisker.lacave.net!lacave.net!not-for-mail
From Jeremy Bopp <jeremy@bopp.net>
Newsgroups comp.lang.ruby
Subject Re: newbie to using builder(for xml markup)
Date Fri, 6 May 2011 10:06:07 -0500
Organization Service de news de lacave.net
Lines 70
Message-ID <4DC40E5C.2040501@bopp.net> (permalink)
References <8c2481d112417ddf26d13fb9716372c6@ruby-forum.com> <157f745306ab284fab9c037eb8763262@ruby-forum.com>
NNTP-Posting-Host bristol.highgroove.com
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding 7bit
X-Trace talisker.lacave.net 1304694383 15636 65.111.164.187 (6 May 2011 15:06:23 GMT)
X-Complaints-To abuse@lacave.net
NNTP-Posting-Date Fri, 6 May 2011 15:06:23 +0000 (UTC)
In-Reply-To <157f745306ab284fab9c037eb8763262@ruby-forum.com>
X-Received-From This message has been automatically forwarded from the ruby-talk mailing list by a gateway at comp.lang.ruby. If it is SPAM, it did not originate at comp.lang.ruby. Please report the original sender, and not us. Thanks! For more details about this gateway, please visit: http://blog.grayproductions.net/categories/the_gateway
X-Mail-Count 382749
X-Ml-Name ruby-talk
X-Rubymirror Yes
X-Ruby-Talk <4DC40E5C.2040501@bopp.net>
Xref x330-a1.tempe.blueboxinc.net comp.lang.ruby:4042

Show key headers only | View raw


On 5/6/2011 08:50, Brad Symons wrote:
> Answered BY MYSELF!!
> 
> There appears to be a conflict with using the :id value, if you have a 
> tag attribute named as this, for some reason it inserts it at the very 
> end.
> 
> So using:
> 
> ids.each do |num|
>   xml.programme(:id => num, :alpha => alphas.fetch(ids.index(num)))
> end
> 
> will print:
> <programme alpha="xxx" id="1"/>
> <programme alpha="yyy" id="2"/>
> <programme alpha="zzz" id="3"/>
> 
> but changing, like so:
> 
> ids.each do |num|
>   xml.programme(:num => num, :alpha => alphas.fetch(ids.index(num)))
> end
> 
> will print:
> <programme num="1" alpha="xxx"/>
> <programme num="2" alpha="yyy"/>
> <programme num="3" alpha="zzz"/>
> 
> SO BE CAREFUL WHEN USING "id"

Hello, Brad,

Actually, be careful using hashes (which is what you're implicitly
doing) if you care about key ordering.  The arguments you're passing to
the programme method are converted into a Hash object by Ruby prior to
being passed.  In the Ruby 1.8 series and earlier, the order of the hash
keys is *not* preserved, so your keys may come out in any order when
they're enumerated.  See what happens if you do this:

ids.each do |num|
  xml.programme(:alpha => alphas.fetch(ids.index(num)), :num => num)
end

Most likely you'll end up with the num attribute followed by the alpha
attribute just like your last example even though the order of them is
switched in my example.  If that's not the case, let us know, but you'll
probably have to take a look at the source for builder in order to
figure out why.

It's possible that you'll get what you want if you switch to Ruby 1.9.2.
 The Ruby 1.9 series preserves the order of hash keys based on insertion
order.  You may also be able to do what you want in Ruby 1.8 by either
wrapping or extending Hash to make your own Hash-like object that
preserves key order.

This thread may be helpful for you.  It even includes a potential
implementation for an order preserving Hash if you're interested:

http://www.ruby-forum.com/topic/166075

Here's a gem for such an implementation:

http://rubygems.org/gems/orderedhash

Using that will make your code a little ugly, but it may be worth it if
you're really concerned about the order of the attributes in your XML.

-Jeremy

Back to comp.lang.ruby | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

newbie to using builder(xml) Brad Symons <snomys@hotmail.com> - 2011-05-06 07:28 -0500
  Re: newbie to using builder(for xml markup) Brad Symons <snomys@hotmail.com> - 2011-05-06 08:50 -0500
    Re: newbie to using builder(for xml markup) Jeremy Bopp <jeremy@bopp.net> - 2011-05-06 10:06 -0500
  Re: newbie to using builder(for xml markup) 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-06 12:33 -0500

csiph-web