Discussion: Modifying a hash key
Afficher un message
Vieux 16/09/2007, 10h17   #2
Thibaut Barrère
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Modifying a hash key

Hi,

I'd also be interested in renaming one or more hash keys. My use case:
ActiveWarehouse-ETL (http://activewarehouse.rubyforge.org/) is an ETL
tool handling rows of data as Hash, and it's very common to have to
rename a field in that case. So far here's what I do:

# rename a bunch of fields
def rename_fields!(row,fields_to_rename,fields_new_nam es)
throw "Array size mismatch" unless fields_new_names.size ==
fields_to_rename.size
mapping = Hash[*fields_to_rename.zip(fields_new_names).flatten]
mapping.each { |old_name,new_name| row[new_name] = row[old_name];
row.delete(old_name) }
end

Does anyone know a built-in way of achieving something similar ?

best

Thibaut

  Réponse avec citation
 
Page generated in 0,06949 seconds with 9 queries