if == any item in an array

1

12 34

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.

I tried
when PhotoEndings.any
and
when PhotoEndings.or

Wild guesses, but they don't work.
 
H

Harold Hausman

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.

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
 
R

Rick DeNatale

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.

I tried
when PhotoEndings.any
and
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
 
E

Ezra Zygmuntowicz

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


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>
end

Cheers-

-- Ezra Zygmuntowicz
-- Lead Rails Evangelist
-- (e-mail address removed)
-- Engine Yard, Serious Rails Hosting
-- (866) 518-YARD (9273)
 
T

Tim Pease

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


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
 
J

Jeremy Hinegardner

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
 
1

12 34

Tim said:
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.
 
S

Sebastian Hungerecker

12 said:
Tim Pease wrote:
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
 
M

Mariusz Pękala

--gKMricLos+KVdGMg
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

Thank you and all the others that answered. The array.include was what I= =20
was thinking too. But removing a couple of commas and adding a couple of= =20
astericks (splats) is nice. Ruby is great.
=20
I don't understand the meaning of the *. I imagine it comes from the=20
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 =3D 10_000_000
STEP =3D LIMIT / 10 + 1
BIG_ARRAY =3D (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 =3D 100_000_000, and the proportions are the same.

--=20
Ceterum censeo Internet Explorer esse delendam.

--gKMricLos+KVdGMg
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7-ecc0.1.6 (GNU/Linux)

iD8DBQFGekrAsnU0scoWZKARAhOnAKDApJD6zJDnIXcDjrQu6eYhr+wNvwCcDHvY
sdEQycx8P27MYOOsQn9R54M=
=qmqg
-----END PGP SIGNATURE-----

--gKMricLos+KVdGMg--
 
R

Robert Klemme

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top