简体   繁体   English

为什么 heroku 会改变哈希的顺序?

[英]why does heroku change the order of hashes?

for the life of me i could not figure this out.对于我的生活,我无法弄清楚这一点。

if i have this in my local dev/production:如果我在本地开发/生产中有这个:

fields = {:name => {...}, :description => {...}, :amount => {.... } }字段 = {:name => {...}, :description => {...}, :amount => {.... } }

its fine i loop through my hashes and print the fields name just like how i declared it.很好,我遍历我的哈希并打印字段名称,就像我声明它的方式一样。

In heroku, the sequence is different in printed order?? heroku中,打印顺序不同?? I have complete no idea why and i can't get a clue why heroku is printing them in different order.我完全不知道为什么,我也不知道为什么 heroku 以不同的顺序打印它们。

does that make sense?那有意义吗?

Edit: OMG its driving me nuts.编辑:天哪,它让我发疯。 I actually do this in my templat, sorting my variable but For some reaaaaaly weird reason, its still coming out in a manner heroku only understands.我实际上是在我的模板中执行此操作,对我的变量进行排序,但出于某种奇怪的原因,它仍然以 heroku 只能理解的方式出现。

- @form_columns = @form_columns.sort_by do |i| 
  - if i[1][:rank].nil?
    - i[1][:rank] = rank
  - i[1][:rank]  
  - rank = rank + 1 

This is how i loop by the way:这就是我循环的方式:

  • @form_columns.each do |column_name| @form_columns.each 做 |column_name|

Please understand that there is no issue in my local production/dev server.请理解我的本地生产/开发服务器没有问题。

The order of keys within a Hash is not guaranteed to equal the order in which were inserted, in Ruby 1.8 Hash 中的键顺序不能保证与插入的顺序相同,在 Ruby 1.8

That behaviour is different in Ruby 1.9, where the order is preserved.这种行为在 Ruby 1.9 中有所不同,其中保留了顺序。

http://www.igvita.com/2009/02/04/ruby-19-internals-ordered-hash/ http://www.igvita.com/2009/02/04/ruby-19-internals-ordered-hash/

Are you using ruby 1.8?您使用的是 ruby 1.8 吗?

If you for some reason have to use Ruby 1.8 then you can get an ordered hash by using OrderedHash in Rails ActiveSupport .如果您出于某种原因必须使用 Ruby 1.8,那么您可以通过在 Rails ActiveSupport中使用OrderedHash来获得订购的 hash。

To see what stack you are using on Heroku:要查看您在 Heroku 上使用的堆栈

heroku stack

and to migrate to 1.9.2:并迁移到 1.9.2:

heroku stack:migrate bamboo-mri-1.9.2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM