<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
J&ouml;rg W Mittag wrote:
<blockquote cite="mid:1eyvrc2l72isk$.dlg / jwmittag.my-fqdn.de"
 type="cite">
  <pre wrap="">S.D wrote:
  </pre>
  <blockquote type="cite">
    <pre wrap="">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:
    </pre>
  </blockquote>
  <pre wrap=""><!---->[...]
  </pre>
  <blockquote type="cite">
    <pre wrap="">public function getTypes($group_type=false) {
  if(!$group_type) {
    return $this-&gt;types;
  }
  else {
    if(array_key_exists($group_type,$this-&gt;groups)) {
      foreach($this-&gt;types as $key =&gt; $mt) {
        if(in_array($key,$this-&gt;groups[$group_type])) {
          $types[$key] = $mt;
        }
      }
      return $types;
    }
    else {
      return false;
    }
  }
}

    </pre>
  </blockquote>
  <pre wrap=""><!---->
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
  </pre>
</blockquote>
<font face="Arial">Hi Joerg,<br>
<br>
Great example! I don't quite understand the pointer(*) notation you are
using in ruby. How would "Hash[*TYPES.select" differ from
"Hash[TYPES.select"?<br>
Is one a pointer to the Hash and the other the Hash itself? I thought
Ruby did not have pointers. When I remove the '*', I get the following
error in irb:<br>
<br>
ArgumentError: odd number of arguments for Hash<br>
<br>
Can you tell me what the '*' before the TYPES in your example changes
the reference to TYPES?<br>
<br>
Also, I understand that you've converted TYPES and GROUPS to global
Hashes, but the same notation should work if these globals were
embedded in a class.<br>
<br>
Assuming class members @types is a hash of mime types =&gt; mime
descriptions and @groups is a hash of group type names =&gt; [array of
file extensions]<br>
</font><font face="Arial"><br>
My final result, that appears to work as the PHP function did, is this:<br>
<br>
</font><tt>def getTypes(group_type=nil)<br>
&nbsp; return @types unless group_type<br>
&nbsp; return nil unless @groups.include? group_type<br>
&nbsp; Hash[*@types.select {|suffix_name,mime_type|
@groups[group_type].include? suffix}.flatten]<br>
end<br>
</tt><font face="Arial"><br>
<font face="Helvetica, Arial, sans-serif">Thanks for your great help.<br>
<br>
-- Steve<br>
<br>
P.S. - Do you have any spare cycles to take on some Ruby consulting
work?<br>
</font></font>
</body>
</html>