PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Ruby hashes
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Ruby hashes

Réponse
 
LinkBack Outils de la discussion
Vieux 22/11/2007, 15h54   #1
Alvaro Perez
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Ruby hashes

Is there a way in Ruby to pass a hash value from string (or whatever) to
an Array dinamically?

Example: An array of objects Teacher, each with his name and belonging
school. After iterating though its elements, we want an output of the
type, "school_name_A" => "teacher_name_A", "school_name_B" =>
["teacher_name_B", "teacher_name_C"].

...or maybe there is another simpler way of doing this?
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 22/11/2007, 16h02   #2
Alex Young
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Ruby hashes

Alvaro Perez wrote:
> Is there a way in Ruby to pass a hash value from string (or whatever) to
> an Array dinamically?
>
> Example: An array of objects Teacher, each with his name and belonging
> school. After iterating though its elements, we want an output of the
> type, "school_name_A" => "teacher_name_A", "school_name_B" =>
> ["teacher_name_B", "teacher_name_C"].

It's easier if you allow:

{"school_name_A" => ["teacher_name_A"],
"school_name_B" => ["teacher_name_B", "teacher_name_C"]}

That way you can do this:

schools = Hash.new{|h,k| h[k] = []}
teachers.each {|teacher| schools[teacher.school] << teacher.name}

--
Alex

  Réponse avec citation
Vieux 22/11/2007, 16h03   #3
Sebastian Hungerecker
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Ruby hashes

Alvaro Perez wrote:
> Example: An array of objects Teacher, each with his name and belonging
> school. After iterating though its elements, we want an output of the
> type, "school_name_A" => "teacher_name_A", "school_name_B" =>
> ["teacher_name_B", "teacher_name_C"].


hash = Hash.new {|h,k| h[k] = []}
teachers.each do |teacher|
hash[teacher.school] << teacher.name
end


HTH,
Sebastian
--
NP: Explosions in the Sky - Snow And Lights
Jabber: sepp2k@jabber.org
ICQ: 205544826

  Réponse avec citation
Vieux 22/11/2007, 16h13   #4
Alvaro Perez
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Ruby hashes

Wow, exactly same solution

but it works! thank you very much to both.
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 14h08.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,09498 seconds with 12 queries