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


Groups > comp.lang.ruby > #4036 > unrolled thread

newbie to using builder(xml)

Started byBrad Symons <snomys@hotmail.com>
First post2011-05-06 07:28 -0500
Last post2011-05-06 12:33 -0500
Articles 4 — 3 participants

Back to article view | Back to comp.lang.ruby


Contents

  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

#4036 — newbie to using builder(xml)

FromBrad Symons <snomys@hotmail.com>
Date2011-05-06 07:28 -0500
Subjectnewbie to using builder(xml)
Message-ID<8c2481d112417ddf26d13fb9716372c6@ruby-forum.com>
Hi,

I am just trying to understand how builder sorts the content of an xml
node, because no matter how I arrange the options, they appears sorted
differently in the irb.

ids = ["1","2","3"]
alphas = ["xxx","yyy","zzz"]

xml = Builder::XmlMarkup.new( :target => $stdout, :indent => 2 )

xml.instruct! :xml, :version => "1.1", :encoding => "US-ASCII"

ids.each do |num|

  xml.programme(:alpha => alphas.fetch(ids.index(num)), :id => num)

end

This prints:
<programme alpha="xxx" id="1"/>
<programme alpha="yyy" id="2"/>
<programme alpha="zzz" id="3"/>

But if I re-arrange the positioning to:

xml.programme(:id => num, :alpha => alphas.fetch(ids.index(num)))

It still produces:
<programme alpha="xxx" id="1"/>
<programme alpha="yyy" id="2"/>
<programme alpha="zzz" id="3"/>

NO CHANGE!!!

Also, is there an easy way to map the values of the arrays together,
than using the fetch method?

THANKS IN ADVANCE!!

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [next] | [standalone]


#4039 — Re: newbie to using builder(for xml markup)

FromBrad Symons <snomys@hotmail.com>
Date2011-05-06 08:50 -0500
SubjectRe: newbie to using builder(for xml markup)
Message-ID<157f745306ab284fab9c037eb8763262@ruby-forum.com>
In reply to#4036
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"

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#4042 — Re: newbie to using builder(for xml markup)

FromJeremy Bopp <jeremy@bopp.net>
Date2011-05-06 10:06 -0500
SubjectRe: newbie to using builder(for xml markup)
Message-ID<4DC40E5C.2040501@bopp.net>
In reply to#4039
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

[toc] | [prev] | [next] | [standalone]


#4049 — Re: newbie to using builder(for xml markup)

From7stud -- <bbxx789_05ss@yahoo.com>
Date2011-05-06 12:33 -0500
SubjectRe: newbie to using builder(for xml markup)
Message-ID<41880a54835470e1996c9e43948b0dab@ruby-forum.com>
In reply to#4036
Brad Symons wrote in post #997015:
>
> I am just trying to understand how builder sorts the content of an xml
> node, because no matter how I arrange the options, they appears sorted
> differently in the irb.

1) The ordering of an xml element's attributes is irrelevant.

2) There are enough inconsistencies between irb and a real ruby program 
that using irb to troubleshoot code is a waste of time.



>
> ids = ["1","2","3"]
> alphas = ["xxx","yyy","zzz"]
>
> xml = Builder::XmlMarkup.new( :target => $stdout, :indent => 2 )
> xml.instruct! :xml, :version => "1.1", :encoding => "US-ASCII"
>
> ids.each do |num|
>   xml.programme(:alpha => alphas.fetch(ids.index(num)), :id => num)
> end
>
> Also, is there an easy way to map the values of the arrays together,
> than using the fetch method?
>

1)

ids.each_index do |i|
  xml.programme(:alpha => alphas[i], :id => ids[i])
end


2)

ids = ["1","2","3"]
alphas = ["xxx","yyy","zzz"]

h = Hash[*alphas.zip(ids).flatten]

p h

--output:--
{"xxx"=>"1", "yyy"=>"2", "zzz"=>"3"}

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.ruby


csiph-web