From: "S.D" Date: 2008-05-30T06:57:28+09:00 Subject: PHP to Ruby (associative array to Hash) --------------070807090203040007070000 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit 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: $this->types = array( 'aac' => 'audio/x-aac', 'ai' => 'application/postscript', 'aif' => 'audio/x-aiff', 'aiff' => 'audio/x-aiff', 'asf' => 'video/x-ms-asf', 'asx' => 'video/x-ms-asx', 'avi' => 'video/avi' ); $groups['office'] = array('csv','doc','dot','pdf','pot','pps','ppt','rtf','txt','xls'); $groups['image'] = array('ai','bmp','dxf','eps','gif','ico','jpg','jpe','jpeg','pdf','png','ps','swf','tif','tiff','wmf'); $groups['compressed'] = array('bin','bz','bz2','gz','sit','tar','tgz','z','zip'); $groups['video'] = array('asf','asx','avi','mov','mpg','mpeg','mp4','qt','ra','ram','swf','wmv'); $groups['audio'] = array('mp3','m4a','ra','ram','wav','wma'); $groups['web'] = array('css','gif','ico','jpg','jpeg','js','htm','html','pdf','php','phps','png','shtml','sql'); $groups['media'] = array('mp3','jpg','mpg','mpeg','vob','avi','wma','wmv','bmp','jpeg','aac','wav'); /* * Return array of mime types * * @param string/bool $group_type * @return array */ 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; } } } The current Ruby translated function: types = { 'aac' => 'audio/x-aac', 'ai' => 'application/postscript', 'aif' => 'audio/x-aiff', 'aiff' => 'audio/x-aiff', 'asf' => 'video/x-ms-asf', 'asx' => 'video/x-ms-asx', 'avi' => 'video/avi' } groups = {'office' => ['csv','doc','dot','pdf','pot','pps','ppt','rtf','txt','xls'], 'image' => ['ai','bmp','dxf','eps','gif','ico','jpg','jpe','jpeg','pdf','png','ps','swf','tif','tiff','wmf'], 'compressed' => ['bin','bz','bz2','gz','sit','tar','tgz','z','zip'], 'video' => ['asf','asx','avi','mov','mpg','mpeg','mp4','qt','ra','ram','swf','wmv'], 'audio' => ['mp3','m4a','ra','ram','wav','wma'], 'web' => ['css','gif','ico','jpg','jpeg','js','htm','html','pdf','php','phps','png','shtml','sql'], 'media' => ['mp3','jpg','mpg','mpeg','vob','avi','wma','wmv','bmp','jpeg','aac','wav'] } #--------------------------------- # Return array of mime types #--------------------------------- def getTypes(grp_type) res_types=Hash.new if(!grp_type) then @types else if(groups.has_key?(grp_type)) then types.each do |k,v| if(groups[grp_type].has_value?(v)) then res_types[k]=v end end else res_types=false end end res_types end --------------070807090203040007070000--