Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #4042
| From | Jeremy Bopp <jeremy@bopp.net> |
|---|---|
| Newsgroups | comp.lang.ruby |
| Subject | Re: newbie to using builder(for xml markup) |
| Date | 2011-05-06 10:06 -0500 |
| Organization | Service de news de lacave.net |
| Message-ID | <4DC40E5C.2040501@bopp.net> (permalink) |
| References | <8c2481d112417ddf26d13fb9716372c6@ruby-forum.com> <157f745306ab284fab9c037eb8763262@ruby-forum.com> |
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 | Next — Previous in thread | Next in thread | Find similar | Unroll 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