Forum: Rails Germany Hash zu groß für Sortierung

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Adam M. (Guest)
on 2009-02-07 23:42
Hallo,

ich möchte eine Länderliste in einer Auswahlliste anbieten. Da gibt es
von Rails ja schon einen schönen Form Helper, leider brauche ich einen
mit deutscher Darstellung und englischen Werten.

Also erstelle ich einen Hash

  def country_list
    return country_list =
{"Afghanistan","Afghanistan","Ägypten","Egypt","Aland","Aland
Islands","Albanien","Albania","Algerien","Algeria","Amerikanische
Jungferninseln","Virgin Islands, U.s.","Amerikanisch-Samoa","American
Samoa","Andorra","Andorra","Angola","Angola","Anguilla","Anguilla","Antarktis","Antarctica","Antigua
und Barbuda","Antigua And Barbuda","Äquatorialguinea,
Republik","Equatorial
Guinea","Argentinien","Argentina","Armenien","Armenia","Aruba","Aruba","Ascension","Ascension","Aserbaidschan","Azerbaijan","Äthiopien","Ethiopia","Australien","Australia","Bahamas","Bahamas","Bahrain","Bahrain","Bangladesch","Bangladesh","Barbados","Barbados","Belgien","Belgium","Belize","Belize","Benin","Benin","Bermuda","Bermuda","Bhutan","Bhutan","Bolivien","Bolivia","Bosnien
und Herzegowina","Bosnia And
Herzegovina","Botswana","Botswana","Bouvetinsel","Bouvet
Island","Brasilien","Brazil","Britische Jungferninseln","Virgin Islands,
British","Brunei","Brunei Darussalam","Bulgarien","Bulgaria","Burkina
Faso","Burkina Faso","Burundi","Burundi","Chile","Chile","China,
Volksrepublik","China","Cookinseln","Cook Islands","Costa Rica","Costa
Rica","Cote d'Ivoire","Cote
D'ivoire","Dänemark","Denmark","Deutschland","Germany","Diego
Garcia","Diego Garcia","Dominica","Dominica","Dominikanische
Republik","Dominican
Republic","Dschibuti","Djibouti","Ecuador","Ecuador","El Salvador","El
Salvador","Eritrea","Eritrea","Estland","Estonia","Falklandinseln","Falkland
Islands (malvinas)","Färöer","Faroe
Islands","Fidschi","Fiji","Finnland","Finland","Frankreich","France","Französische
Süd- und Antarktisgebiete","French Southern
Territories","Französisch-Guayana","French
Guiana","Französisch-Polynesien","French
Polynesia","Gabun","Gabon","Gambia","Gambia","Georgien","Georgia","Ghana,
Republik","Ghana","Gibraltar","Gibraltar","Grenada","Grenada","Griechenland","Greece","Grönland","Greenland","Guadeloupe","Guadeloupe","Guam","Guam","Guatemala","Guatemala","Guernsey,
Vogtei","Guernsey","Guinea, Republik","Guinea","Guinea-Bissau,
Republik","Guinea-bissau","Guyana","Guyana","Haiti","Haiti","Heard und
McDonaldinseln","Heard Island And Mcdonald
Islands","Honduras","Honduras","Hongkong","Hong
Kong","Indien","India","Indonesien","Indonesia","Insel Mann","Isle Of
Man","Irak","Iraq","Iran","Iran, Islamic Republic Of","Irland,
Republik","Ireland","Island","Iceland","Israel","Israel","Italien","Italy","Jamaika","Jamaica","Japan","Japan","Jemen","Yemen","Jersey","Jersey","Jordanien","Jordan","Kaimaninseln","Cayman
Islands","Kambodscha","Cambodia","Kamerun","Cameroon","Kanada","Canada","Kap
Verde, Republik","Cape
Verde","Kasachstan","Kazakhstan","Katar","Qatar","Kenia","Kenya","Kirgisistan","Kyrgyzstan","Kiribati","Kiribati","Kokosinseln","Cocos
(keeling) Islands","Kolumbien","Colombia","Komoren","Comoros","Kongo,
Demokratische Republik","Congo, The Democratic Republic Of The","Kongo,
Republik","Congo","Korea, Demokratische Volkrepublik","Korea, Democratic
People's Republic Of","Korea, Republik","Korea, Republic
Of","Kroatien","Croatia","Kuba","Cuba","Kuwait","Kuwait","Laos","Lao
People's Democratic
Republic","Lesotho","Lesotho","Lettland","Latvia","Libanon","Lebanon","Liberia,
Republik","Liberia","Libyen","Libyan Arab Jamahiriya","Liechtenstein,
Fürstentum","Liechtenstein","Litauen","Lithuania","Luxemburg","Luxembourg","Macao","Macao","Madagaskar,
Republik","Madagascar","Malawi,
Republik","Malawi","Malaysia","Malaysia","Malediven","Maldives","Mali,
Republik","Mali","Malta","Malta","Marokko","Morocco","Marshallinseln","Marshall
Islands","Martinique","Martinique","Mauretanien","Mauritania","Mauritius,
Republik","Mauritius","Mayotte","Mayotte","Mazedonien","Macedonia, The
Former Yugoslav Republic Of","Mexiko","Mexico","Mikronesien, Föderierte
Staaten von","Micronesia, Federated States
Of","Moldawien","Moldova","Monaco","Monaco","Mongolei","Mongolia","Montenegro","Montenegro","Montserrat","Montserrat","Mosambik","Mozambique","Myanmar","Myanmar","Namibia,
Republik","Namibia","Nauru","Nauru","Nepal","Nepal","Neukaledonien","New
Caledonia","Neuseeland","New
Zealand","Nicaragua","Nicaragua","Niederlande","Netherlands","Niederländische
Antillen","Netherlands
Antilles","Niger","Niger","Nigeria","Nigeria","Niue","Niue","Nördliche
Marianen","Northern Mariana Islands","Norfolkinsel","Norfolk
Island","Norwegen","Norway","Oman","Oman","Österreich","Austria","Pakistan","Pakistan","Palästinensische
Autonomiegebiete","Palestinian Territory,
Occupied","Palau","Palau","Panama","Panama","Papua-Neuguinea","Papua New
Guinea","Paraguay","Paraguay","Peru","Peru","Philippinen","Philippines","Pitcairninseln","Pitcairn","Polen","Poland","Portugal","Portugal","Puerto
Rico","Puerto Rico","Réunion","Reunion","Ruanda,
Republik","Rwanda","Rumänien","Romania","Russische Föderation","Russian
Federation","Salomonen","Solomon Islands","Sambia,
Republik","Zambia","Samoa","Samoa","San Marino","San Marino","São Tomé
und Príncipe","Sao Tome And Principe","Saudi-Arabien, Königreich","Saudi
Arabia","Schweden","Sweden","Schweiz","Switzerland","Senegal","Senegal","Serbien","Serbia","Seychellen,
Republik der","Seychelles","Sierra Leone, Republik","Sierra
Leone","Simbabwe,
Republik","Zimbabwe","Singapur","Singapore","Slowakei","Slovakia","Slowenien","Slovenia","Somalia,
Demokratische Republik","Somalia","Spanien","Spain","Sri Lanka","Sri
Lanka","St. Helena und Nebengebiete, Kronkolonie ","Saint Helena","St.
Kitts und Nevis","Saint Kitts And Nevis","St. Lucia","Saint Lucia","St.
Pierre und Miquelon","Saint Pierre And Miquelon","St. Vincent und die
Grenadinen (GB)","Saint Vincent And The Grenadines","Südafrika,
Republik","South Africa","Sudan","Sudan","Südgeorgien und die Südlichen
Sandwichinseln","South Georgia And The South Sandwich
Islands","Suriname","Suriname","Svalbard und Jan Mayen","Svalbard And
Jan Mayen","Swasiland","Swaziland","Syrien","Syrian Arab
Republic","Tadschikistan","Tajikistan","Taiwan","Taiwan, Province Of
China","Tansania, Vereinigte Republik","Tanzania, United Republic
Of","Thailand","Thailand","Timor-Leste, Demokratische
Republik","Timor-leste","Togo,
Republik","Togo","Tokelau","Tokelau","Tonga","Tonga","Trinidad und
Tobago","Trinidad And Tobago","Tristan da Cunha","Tristan da
Cunha","Tschad, Republik","Chad","Tschechische Republik","Czech
Republic","Tunesien","Tunisia","Türkei","Turkey","Turkmenistan","Turkmenistan","Turks-
und Caicosinseln","Turks And Caicos Islands","Tuvalu","Tuvalu","Uganda,
Republik","Uganda","Ukraine","Ukraine","Ungarn","Hungary","Uruguay","Uruguay","Usbekistan","Uzbekistan","Vanuatu","Vanuatu","Vatikanstadt","Holy
See (vatican City State)","Venezuela","Venezuela","Vereinigte Arabische
Emirate","United Arab Emirates","Vereinigte Staaten von Amerika","United
States","Vereinigtes Königreich von Großbritannien und
Nordirland","United Kingdom","Vietnam","Viet Nam","Wallis und
Futuna","Wallis And Futuna","Weihnachtsinsel","Christmas
Island","Weißrussland","Belarus","Westsahara","Western
Sahara","Zentralafrikanische Republik","Central African
Republic","Zypern, Republik","Cyprus"}
  end

Das Problem ist, das er mit die Auswahlliste richtig generiert, nur dass
die Liste unsortiert ist.

Wenn ich aber country_list.sort mache, damit er den Hash sortiert,
bekomme ich eine

 SystemStackError in EmpfaengerController#index

stack level too deep

Fehlermeldung, denke mal dass der Hash zu groß ist. Hat jemand eine
andere Idee?
Mein String ist ja schon sortiert, nur im Hash dann nicht mehr.

Gruß

Adam
Thomas r. K. (Guest)
on 2009-02-08 00:34
(Received via mailing list)
Am 07.02.2009 um 22:42 schrieb Adam M.:
>
> SystemStackError in EmpfaengerController#index
>
> stack level too deep
>
> Fehlermeldung, denke mal dass der Hash zu groß ist. Hat jemand eine
> andere Idee?
> Mein String ist ja schon sortiert, nur im Hash dann nicht mehr.

Nein, normalerweise ist das ein Hinweis drauf dass du eine
Endlosschleife hast.

ciao, tom

--
Thomas R. "TomK32" Koll || http://tomk32.de || http://ananasblau.com
just a geek trying to change the world
Skype: TomK32 || Mail: removed_email_address@domain.invalid
http://flickr.com/people/tomk32
Thomas P. (Guest)
on 2009-02-08 00:44
(Received via mailing list)
2009/2/7 Adam M. <removed_email_address@domain.invalid>:
> {"Afghanistan","Afghanistan","Ägypten","Egypt","Aland","Aland
läuft hier (unter reinem Ruby und ohne Rails) einwandfrei. Dein
Problem muß wo anders liegen.

Übrigens: zuerst die Zuweisung an eine lokale Variable und dann return
kannst du dir sparen, warum nicht "return {"Afghanistan",....  }".
Und sogar deine ganze return-Zeile kannst du weglassen - aber das ist
Geschmackssache, manche schreiben lieber ein explizites return.

Gruß
-Thomas


--
Thomas P.
removed_email_address@domain.invalid
http://thopre.googlepages.com/
http://thopre.wordpress.com/
Thomas P. (Guest)
on 2009-02-08 00:48
(Received via mailing list)
Nachtrag:

Ich verwende in einem gem von mir u.U. noch viel, viel größere Hashes.
Daß der Hash zu groß für Ruby an sich wäre, würde ich ausschließen.

-Thomas
Adam M. (Guest)
on 2009-02-08 00:51
Thomas P. wrote:
> 2009/2/7 Adam M. <removed_email_address@domain.invalid>:
>> {"Afghanistan","Afghanistan","�gypten","Egypt","Aland","Aland
> l�uft hier (unter reinem Ruby und ohne Rails) einwandfrei. Dein
> Problem mu� wo anders liegen.
>
> �brigens: zuerst die Zuweisung an eine lokale Variable und dann return
> kannst du dir sparen, warum nicht "return {"Afghanistan",....  }".
> Und sogar deine ganze return-Zeile kannst du weglassen - aber das ist
> Geschmackssache, manche schreiben lieber ein explizites return.
>
> Gru�
> -Thomas
>
>
> --
> Thomas P.
> removed_email_address@domain.invalid
> http://thopre.googlepages.com/
> http://thopre.wordpress.com/

Habe den Fehler entdeckt. das .sort muss erst nach der Erstellung des
Hashes drangehängt werden.

Jetzt landen leider die Einträge mit Umlauten ans Ende.
Wahrscheinlich ist es besser die Liste in ein zweidimensionales Array zu
schreiben, dann hat man Einfluss auf die Reihenfolge, und spart sich die
Rechenzeit fürs sortieren.
Michael S. (Guest)
on 2009-02-08 00:55
(Received via mailing list)
On Saturday 07 February 2009, Adam M. wrote:
> Hallo,
>
> ich möchte eine Länderliste in einer Auswahlliste anbieten. Da gibt
> es von Rails ja schon einen schönen Form Helper, leider brauche ich
> einen mit deutscher Darstellung und englischen Werten.
[...]

Schau dir mal options_for_select an. Da deine Liste ja ohnehin statisch
ist, kannst du sie problemlos in als Array von [Anzeige, Wert]-Arrays
erzeugen.

Ich würde allerdings keine statische Liste verwenden, sondern eine
Datenbank-Tabelle, die die ISO-Ländercodes enthält und die
Bezeichnungen in verschiedenen Sprachen (ISO-Sprachcode). Plus die
Rails-typische ID.

Alternativ könntest -- und vermutlich solltest -- du die I18n-API
nutzen:

  http://guides.rubyonrails.org/i18n.html

Michael

--
Michael S.
mailto:removed_email_address@domain.invalid
http://www.schuerig.de/michael/
Thomas r. K. (Guest)
on 2009-02-08 13:52
(Received via mailing list)
Am 07.02.2009 um 23:51 schrieb Adam M.:
> Habe den Fehler entdeckt. das .sort muss erst nach der Erstellung des
> Hashes drangehängt werden.

Wusst ich's doch.


Thomas R. "TomK32" Koll || http://tomk32.de || http://ananasblau.com
just a geek trying to change the world
Skype: TomK32 || Mail: removed_email_address@domain.invalid
http://flickr.com/people/tomk32
This topic is locked and can not be replied to.