From: "Jörg W Mittag" Date: 2008-05-30T19:31:22+09:00 Subject: Re: PHP to Ruby (associative array to Hash) S.D wrote: > In the following PHP function, both 'types' and 'groups' are associative > arrays. I'm trying to rewrite this function in Ruby, using Hashes and I > haven't yet got it right. If someone with some insight into PHP and Ruby > could give it a look and see where my current code might be incorrect, I > 'd appreciate any pointers that might correct the results. The PHP > function is working; the Ruby version is not (yet) working. TIA for any > tips, criticism and suggestions that you can submit. > > -- Steve > > The original PHP function: [...] > public function getTypes($group_type=false) { > if(!$group_type) { > return $this->types; > } > else { > if(array_key_exists($group_type,$this->groups)) { > foreach($this->types as $key => $mt) { > if(in_array($key,$this->groups[$group_type])) { > $types[$key] = $mt; > } > } > return $types; > } > else { > return false; > } > } > } > Hi Steve! This is my take. I shuffled things around a bit, e.g. using constants instead of variables, but you'll figure it out ... def types group = nil return TYPES unless group return nil unless GROUPS.include? group Hash[*TYPES.select {|suffix, mime_type| GROUPS[group].include? suffix}.flatten] end HTH, jwm