On Thu, Jun 21, 2007 at 04:40:34AM +0900, Tim Pease wrote:
> On 6/20/07, 12 34 <rubyforum / web.knobby.ws> wrote:
> >I'd like to write something like
> >
> >PhotoEndings = %w[JPG,MRW,JPE]
> >MovieEndings = %w[AVI]
> >case ext # determined from the photo file name
> >when PhotoEndings
> ><do something whenever ext is the same as any of the photo endings. >
> >when MovieEndings
> ><do something else>
> >else
> >end
> >
> >I don't have to use case, any conditional will do.
> >
> >Thanks. I hope I have explained this. I have no idea what to search for.
> >I did try some, but I'm a newbie and don't understand the more advanced
> >syntax.
> >
> 
> PhotoEndings = %w(JPG MRW JPE)
> MovieEndings = %w(AVI)
> 
> case ext.upcase
> when *PhotoEndings
>  puts "I'm a photo!"
> when *MovieEndings
>  puts "I'm a movie!"
> else
>  puts "I'm unknown '#{ext}'"
> end
> 

Or take a completely different approach to the issue, use the mime/types
gem.

    require 'rubygems' 
    require 'mime/types' 

    # add in a non-standard mime type for MRW files
    mrw_mime_type = MIME::Type.from_array(['image/x-raw', %w[ mrw ]])
    MIME::Types.add(mrw_mime_type)

    test_files = %w[ jpeg_file.jpg jpeg_file.jpe mrw_file.mrw avi_file.avi mov_file.mov jkl_file.jkl ]

    test_files.each do |t| 
        mime_types_for_t = MIME::Types.of(t)
       
        if mime_types_for_t.size > 0 then
            case mime_types_for_t.first.raw_media_type
            when "image"
                puts "#{t} is an image!"
            when "video"
                puts "#{t} is a video!"
            else
                puts "#{t} is a #{mt.raw_media_type}"
            end 
        else
            puts "No mime types found for #{t}"
        end 
    end

output:

    % ruby mrw-example.rb
    jpeg_file.jpg is an image!
    jpeg_file.jpe is an image!
    mrw_file.mrw is an image!
    avi_file.avi is a video!
    mov_file.mov is a video!
    No mime types found for jkl_file.jkl

enjoy,

-jeremy

-- 
========================================================================
 Jeremy Hinegardner                              jeremy / hinegardner.org