From: 7stud -- Date: 2011-05-07T02:33:33+09:00 Subject: Re: newbie to using builder(for xml markup) 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/.