I''m in rails 2.3.4, and i''m trying to convert an array into an ordered hash. Since an ordered hash is kind of an array in disguise i thought this would be simple. But the ordering is breaking down by the time i add the third or fourth pair. Check it out:>> row = [["QID", "8"], ["SID", "21"], ["Question", "Which number is the smallest?"], ["Right", "-300"], ["Wrong1", "-3"], ["Wrong2", "-13"], ["Wrong3", "-30"], ["50_50", "1"], ["Checked", nil], ["DBand5-7", "NA"], ["DBand7-9", "NA"], ["DBand9-11", "H"], ["DBand11-14", "E"], ["DBand14-16", nil], ["Editor", nil], ["Subject", "Mathematics"], ["Question_Type", "curriculum"], ["Keywords", "\"eggs, bacon, sausage and chips\", reading and writing, arithmetic"], ["UserLogin", nil], ["Privacy", nil], ["Official", nil]]=> [["QID", "8"], ["SID", "21"], ["Question", "Which number is the smallest?"], ["Right", "-300"], ["Wrong1", "-3"], ["Wrong2", "-13"], ["Wrong3", "-30"], ["50_50", "1"], ["Checked", nil], ["DBand5-7", "NA"], ["DBand7-9", "NA"], ["DBand9-11", "H"], ["DBand11-14", "E"], ["DBand14-16", nil], ["Editor", nil], ["Subject", "Mathematics"], ["Question_Type", "curriculum"], ["Keywords", ""eggs, bacon, sausage and chips", reading and writing, arithmetic"], ["UserLogin", nil], ["Privacy", nil], ["Official", nil]]>> ohash = ActiveSupport::OrderedHash.new=> #<OrderedHash {}>>> row.each{|pair| ohash[pair.first] = pair.last;puts "\n#{ohash.inspect}"};false#<OrderedHash {"QID"=>"8"}> #<OrderedHash {"QID"=>"8", "SID"=>"21"}> #<OrderedHash {"QID"=>"8", "SID"=>"21", "Question"=>"Which number is the smallest?"}> #<OrderedHash {"QID"=>"8", "SID"=>"21", "Right"=>"-300", "Question"=>"Which number is the smallest?"}> Here i''m onto the fourth pair and already the order is screwed up: "Right" has jumped in before "Question". Can anyone explain what''s going wrong here? thanks - max ps i don''t want to (re)start a debate on the merits (or lack thereof) of ordered hashes thanks! Just want to understand this particular problem. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
-- I got the same results - ''dictionary'' gem works properly - The library seems broken - this is an opportunity to write a test case - identify the problem and submit a patch - first check lighthouse for a ticket. Maybe the rails core team wants to rename Dictionary as OrderedHash Dictionary is from the Ruby Facet''s project. -- -- gem install dictionary [8:25:34] Successfully installed dictionary-1.0.0 1 gem installed Installing ri documentation for dictionary-1.0.0... Installing RDoc documentation for dictionary-1.0.0... ./script/console [8:25:43] Loading development environment (Rails 2.3.8) ree-1.8.7-2010.01 > require ''dictionary'' ree-1.8.7-2010.01 > row = [["QID", "8"], ["SID", "21"], ["Question", "Which number is the smallest?"], ["Right", "-300"], ["Wrong1", "-3"], ["Wrong2", "-13"], ["Wrong3", "-30"], ["50_50", "1"], ["Checked", nil], ["DBand5-7", "NA"], ["DBand7-9", "NA"], ["DBand9-11", "H"], ["DBand11-14", "E"], ["DBand14-16", nil], ["Editor", nil], ["Subject", "Mathematics"], ["Question_Type", "curriculum"], ["Keywords", "\"eggs, bacon, sausage and chips\", reading and writing, arithmetic"], ["UserLogin", nil], ["Privacy", nil], ["Official", nil]] d = Dictionary.new => {} ree-1.8.7-2010.01 > row.each {|e| d[e.first] = e.last} => [["QID", "8"], ["SID", "21"], ["Question", "Which number is the smallest?"], ["Right", "-300"], ["Wrong1", "-3"], ["Wrong2", "-13"], ["Wrong3", "-30"], ["50_50", "1"], ["Checked", nil], ["DBand5-7", "NA"], ["DBand7-9", "NA"], ["DBand9-11", "H"], ["DBand11-14", "E"], ["DBand14-16", nil], ["Editor", nil], ["Subject", "Mathematics"], ["Question_Type", "curriculum"], ["Keywords", "\"eggs, bacon, sausage and chips\", reading and writing, arithmetic"], ["UserLogin", nil], ["Privacy", nil], ["Official", nil]] On Thu, Jun 10, 2010 at 8:09 AM, Max Williams <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> I''m in rails 2.3.4, and i''m trying to convert an array into an ordered > hash. Since an ordered hash is kind of an array in disguise i thought > this would be simple. But the ordering is breaking down by the time i > add the third or fourth pair. Check it out: > >>> row = [["QID", "8"], ["SID", "21"], ["Question", "Which number is the smallest?"], ["Right", "-300"], ["Wrong1", "-3"], ["Wrong2", "-13"], ["Wrong3", "-30"], ["50_50", "1"], ["Checked", nil], ["DBand5-7", "NA"], ["DBand7-9", "NA"], ["DBand9-11", "H"], ["DBand11-14", "E"], ["DBand14-16", nil], ["Editor", nil], ["Subject", "Mathematics"], ["Question_Type", "curriculum"], ["Keywords", "\"eggs, bacon, sausage and chips\", reading and writing, arithmetic"], ["UserLogin", nil], ["Privacy", nil], ["Official", nil]] > => [["QID", "8"], ["SID", "21"], ["Question", "Which number is the > smallest?"], ["Right", "-300"], ["Wrong1", "-3"], ["Wrong2", "-13"], > ["Wrong3", "-30"], ["50_50", "1"], ["Checked", nil], ["DBand5-7", "NA"], > ["DBand7-9", "NA"], ["DBand9-11", "H"], ["DBand11-14", "E"], > ["DBand14-16", nil], ["Editor", nil], ["Subject", "Mathematics"], > ["Question_Type", "curriculum"], ["Keywords", ""eggs, bacon, sausage and > chips", reading and writing, arithmetic"], ["UserLogin", nil], > ["Privacy", nil], ["Official", nil]] >>> ohash = ActiveSupport::OrderedHash.new > => #<OrderedHash {}> >>> row.each{|pair| ohash[pair.first] = pair.last;puts "\n#{ohash.inspect}"};false > > #<OrderedHash {"QID"=>"8"}> > > #<OrderedHash {"QID"=>"8", "SID"=>"21"}> > > #<OrderedHash {"QID"=>"8", "SID"=>"21", "Question"=>"Which number is the > smallest?"}> > > #<OrderedHash {"QID"=>"8", "SID"=>"21", "Right"=>"-300", > "Question"=>"Which number is the smallest?"}> > > Here i''m onto the fourth pair and already the order is screwed up: > "Right" has jumped in before "Question". > > Can anyone explain what''s going wrong here? thanks - max > > ps i don''t want to (re)start a debate on the merits (or lack thereof) of > ordered hashes thanks! Just want to understand this particular problem. > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
{"QID"=>"8", "SID"=>"21", "Question"=>"Which number is the smallest?", "Right"=>"-300", "Wrong1"=>"-3", "Wrong2"=>"-13", "Wrong3"=>"-30", "50_50"=>"1", "Checked"=>nil, "DBand5-7"=>"NA", "DBand7-9"=>"NA", "DBand9-11"=>"H", "DBand11-14"=>"E", "DBand14-16"=>nil, "Editor"=>nil, "Subject"=>"Mathematics", "Question_Type"=>"curriculum", "Keywords"=>"\"eggs, bacon, sausage and chips\", reading and writing, arithmetic", "UserLogin"=>nil, "Privacy"=>nil, "Official"=>nil} On Thu, Jun 10, 2010 at 8:30 AM, Curtis Schofield <curtis-DDU1nqEjlGkHaT8GDLgCUg@public.gmane.org> wrote:> -- I got the same results - ''dictionary'' gem works properly - > > The library seems broken - this is an opportunity to write a test > case - identify the problem and submit a patch - first check > lighthouse for a ticket. > > Maybe the rails core team wants to rename Dictionary as OrderedHash > > Dictionary is from the Ruby Facet''s project. > -- > -- > > gem install dictionary > > [8:25:34] > Successfully installed dictionary-1.0.0 > 1 gem installed > Installing ri documentation for dictionary-1.0.0... > Installing RDoc documentation for dictionary-1.0.0... > > ./script/console > > [8:25:43] > Loading development environment (Rails 2.3.8) > ree-1.8.7-2010.01 > require ''dictionary'' > ree-1.8.7-2010.01 > row = [["QID", "8"], ["SID", "21"], ["Question", > "Which number is the smallest?"], ["Right", "-300"], ["Wrong1", "-3"], > ["Wrong2", "-13"], ["Wrong3", "-30"], ["50_50", "1"], ["Checked", > nil], ["DBand5-7", "NA"], ["DBand7-9", "NA"], ["DBand9-11", "H"], > ["DBand11-14", "E"], ["DBand14-16", nil], ["Editor", nil], ["Subject", > "Mathematics"], ["Question_Type", "curriculum"], ["Keywords", "\"eggs, > bacon, sausage and chips\", reading and writing, arithmetic"], > ["UserLogin", nil], ["Privacy", nil], ["Official", nil]] > d = Dictionary.new > => {} > > ree-1.8.7-2010.01 > row.each {|e| d[e.first] = e.last} > => [["QID", "8"], ["SID", "21"], ["Question", "Which number is the > smallest?"], ["Right", "-300"], ["Wrong1", "-3"], ["Wrong2", "-13"], > ["Wrong3", "-30"], ["50_50", "1"], ["Checked", nil], ["DBand5-7", > "NA"], ["DBand7-9", "NA"], ["DBand9-11", "H"], ["DBand11-14", "E"], > ["DBand14-16", nil], ["Editor", nil], ["Subject", "Mathematics"], > ["Question_Type", "curriculum"], ["Keywords", "\"eggs, bacon, sausage > and chips\", reading and writing, arithmetic"], ["UserLogin", nil], > ["Privacy", nil], ["Official", nil]] > > On Thu, Jun 10, 2010 at 8:09 AM, Max Williams <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> I''m in rails 2.3.4, and i''m trying to convert an array into an ordered >> hash. Since an ordered hash is kind of an array in disguise i thought >> this would be simple. But the ordering is breaking down by the time i >> add the third or fourth pair. Check it out: >> >>>> row = [["QID", "8"], ["SID", "21"], ["Question", "Which number is the smallest?"], ["Right", "-300"], ["Wrong1", "-3"], ["Wrong2", "-13"], ["Wrong3", "-30"], ["50_50", "1"], ["Checked", nil], ["DBand5-7", "NA"], ["DBand7-9", "NA"], ["DBand9-11", "H"], ["DBand11-14", "E"], ["DBand14-16", nil], ["Editor", nil], ["Subject", "Mathematics"], ["Question_Type", "curriculum"], ["Keywords", "\"eggs, bacon, sausage and chips\", reading and writing, arithmetic"], ["UserLogin", nil], ["Privacy", nil], ["Official", nil]] >> => [["QID", "8"], ["SID", "21"], ["Question", "Which number is the >> smallest?"], ["Right", "-300"], ["Wrong1", "-3"], ["Wrong2", "-13"], >> ["Wrong3", "-30"], ["50_50", "1"], ["Checked", nil], ["DBand5-7", "NA"], >> ["DBand7-9", "NA"], ["DBand9-11", "H"], ["DBand11-14", "E"], >> ["DBand14-16", nil], ["Editor", nil], ["Subject", "Mathematics"], >> ["Question_Type", "curriculum"], ["Keywords", ""eggs, bacon, sausage and >> chips", reading and writing, arithmetic"], ["UserLogin", nil], >> ["Privacy", nil], ["Official", nil]] >>>> ohash = ActiveSupport::OrderedHash.new >> => #<OrderedHash {}> >>>> row.each{|pair| ohash[pair.first] = pair.last;puts "\n#{ohash.inspect}"};false >> >> #<OrderedHash {"QID"=>"8"}> >> >> #<OrderedHash {"QID"=>"8", "SID"=>"21"}> >> >> #<OrderedHash {"QID"=>"8", "SID"=>"21", "Question"=>"Which number is the >> smallest?"}> >> >> #<OrderedHash {"QID"=>"8", "SID"=>"21", "Right"=>"-300", >> "Question"=>"Which number is the smallest?"}> >> >> Here i''m onto the fourth pair and already the order is screwed up: >> "Right" has jumped in before "Question". >> >> Can anyone explain what''s going wrong here? thanks - max >> >> ps i don''t want to (re)start a debate on the merits (or lack thereof) of >> ordered hashes thanks! Just want to understand this particular problem. >> -- >> Posted via http://www.ruby-forum.com/. >> >> -- >> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. >> To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >> >> >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jun 10, 4:09 pm, Max Williams <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Here i''m onto the fourth pair and already the order is screwed up: > "Right" has jumped in before "Question". > > Can anyone explain what''s going wrong here? thanks - maxOrderedHash subclasses from Hash, and the inspect method wasn''t overriden to inspect things in order (mea culpa!). if you iterate with each (or look at the keys) then you should see that things are in the right order Fred> > ps i don''t want to (re)start a debate on the merits (or lack thereof) of > ordered hashes thanks! Just want to understand this particular problem. > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung wrote:> OrderedHash subclasses from Hash, and the inspect method wasn''t > overriden to inspect things in order (mea culpa!). if you iterate with > each (or look at the keys) then you should see that things are in the > right order > > Fredahhh...that was you? :) Yep, on closer inspection they are in the right order, and i monkeypatched the inspect method and it seems fine now: module ActiveSupport class OrderedHash def inspect "{" << self.collect{|k,v| "#{k.inspect} => #{v.inspect}"}.join(", ") << "}" end end end thanks for clearing that up! max -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.