If == any item in an array

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

when MovieEndings

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.

I tried
when PhotoEndings.any
and
when PhotoEndings.or

Wild guesses, but they don’t work.

On 6/20/07, 12 34 [email protected] wrote:

end

I don’t have to use case, any conditional will do.

I think you’re looking for Array#include?

$ ri Array#include?
--------------------------------------------------------- Array#include?
array.include?(obj) → true or false

 Returns +true+ if the given object is present in _self_ (that is,
 if any object +==+ _anObject_), +false+ otherwise.

    a = [ "a", "b", "c" ]
    a.include?("b")   #=> true
    a.include?("z")   #=> false

Hope that helps,
-Harold

On 6/20/07, 12 34 [email protected] wrote:

end
when PhotoEndings.or

case
when PhotoEndings.include?(ext)

when MovieEndings.include?(ext)

end

The form
case x
when y
#do something
when z
#do something else
end

is actually equivalent to:

if y === x
then
#do something
elsif z === x
# do something else
end

and

case #no value
when y
# do something
when z
# do something else
end

if y
then
#do something
elsif z
# do something else
end


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

On Jun 20, 2007, at 12:07 PM, 12 34 wrote:

PhotoEndings = %w[JPG,MRW,JPE]
MovieEndings = %w[AVI]
case ext # determined from the photo file name
when PhotoEndings

when MovieEndings

else
end

PhotoEndings = %w[JPG,MRW,JPE]
MovieEndings = %w[AVI]

case ext # determined from the photo file name
when *PhotoEndings

when *MovieEndings

end

Cheers-

– Ezra Z.
– Lead Rails Evangelist
[email protected]
– Engine Y., Serious Rails Hosting
– (866) 518-YARD (9273)

2007/6/20, 12 34 [email protected]:

else
and
when PhotoEndings.or

Wild guesses, but they don’t work.


Posted via http://www.ruby-forum.com/.

You could use regular expressions:

PhotoEndings = /JPG|MRW|JPE/i
MovieEndings = /AVI/i

case ext
when PhotoEndings

when MovieEndings

else

end

Regards,
Raf

On 6/20/07, 12 34 [email protected] wrote:

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

When you were declaring your endings with the %w notation, you had put
commas between the file extensions. Therefore, your arrays only
contained one element.

Give this code a shot. You almost had it – the syntax you were
looking for was the leading splat “*” in front of the PhotoEndings and
MovieEndings in the branches of the case statement.

Blessings,
TwP

On Thu, Jun 21, 2007 at 04:40:34AM +0900, Tim P. wrote:

else
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

Tim P. wrote:

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

When you were declaring your endings with the %w notation, you had put
commas between the file extensions. Therefore, your arrays only
contained one element.

Give this code a shot. You almost had it – the syntax you were
looking for was the leading splat “*” in front of the PhotoEndings and
MovieEndings in the branches of the case statement.

Blessings,
TwP
Thank you and all the others that answered. The array.include was what I
was thinking too. But removing a couple of commas and adding a couple of
astericks (splats) is nice. Ruby is great.

I don’t understand the meaning of the *. I imagine it comes from the
wildcard usage. What do I look up in my books to understand this.

Again thanks to the great community. All the answers help me learn.

12 34 wrote:

Tim P. wrote:

end
I don’t understand the meaning of the *. I imagine it comes from the
wildcard usage. What do I look up in my books to understand this.

It has nothing to do with wildcards. The * takes an array and turns it
into
a list of arguments. So args=[a,b,c]; f(*args) is the same as f(a,b,c)
and
when *PhotoEndings is the same as when JPG, MRW, JPE

On 2007-06-21 09:17:24 +0900 (Thu, Jun), 12 34 wrote:

Give this code a shot. You almost had it – the syntax you were
looking for was the leading splat “*” in front of the PhotoEndings and
MovieEndings in the branches of the case statement.

Thank you and all the others that answered. The array.include was what I
was thinking too. But removing a couple of commas and adding a couple of
astericks (splats) is nice. Ruby is great.

I don’t understand the meaning of the *. I imagine it comes from the
wildcard usage. What do I look up in my books to understand this.

When the splat-solution appeared here an alarm bell rung in my head:
If the array is small, like in this problem, everything is OK. But what
if the array is huge?

I was wondering whether stack overflows could occur. I havent found
them, but the benchmark shows that .include?() is faster.

require ‘benchmark’
LIMIT = 10_000_000
STEP = LIMIT / 10 + 1
BIG_ARRAY = (1…LIMIT).to_a; nil

def look_by_splash(item)
case item
when *BIG_ARRAY
‘found by splash’
else
‘not found by splash’
end
end

def look_by_include(item)
case
when BIG_ARRAY.include?(item)
‘found by include’
else
‘not found by include’
end
end

Benchmark.bmbm do |x|
x.report(‘by_splash’) do
1.step(LIMIT + STEP, STEP) { |i| look_by_splash(i) }
end
x.report(‘by_include’) do
1.step(LIMIT + STEP, STEP) { |i| look_by_include(i) }
end
end

Rehearsal ---------------------------------------------
by_splash 13.080000 0.000000 13.080000 ( 13.212631)
by_include 6.750000 0.000000 6.750000 ( 6.817161)
----------------------------------- total: 19.830000sec

            user     system      total        real

by_splash 12.930000 0.000000 12.930000 ( 13.060550)
by_include 6.750000 0.000000 6.750000 ( 6.809274)

I tested with LIMIT = 100_000_000, and the proportions are the same.

On 21.06.2007 11:54, Mariusz Pękala wrote:

wildcard usage. What do I look up in my books to understand this.

When the splat-solution appeared here an alarm bell rung in my head:
If the array is small, like in this problem, everything is OK. But what
if the array is huge?

I was wondering whether stack overflows could occur. I havent found
them, but the benchmark shows that .include?() is faster.

If there are many choices you would probably choose another solution -
namely creating a hash with file extensions as keys and lambdas as
values that perform some operation.

Kind regards

robert