[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

if == any item in an array

12 34

6/20/2007 7:07:00 PM

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.

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

9 Answers

Harold Hausman

6/20/2007 7:16:00 PM

0

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

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

Rick DeNatale

6/20/2007 7:21:00 PM

0

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


--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

Ezra Zygmuntowicz

6/20/2007 7:40:00 PM

0


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
> <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
-- ez@engineyard.com
-- Engine Yard, Serious Rails Hosting
-- (866) 518-YARD (9273)



Tim Pease

6/20/2007 7:41:00 PM

0

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


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

Jeremy Hinegardner

6/20/2007 9:28:00 PM

0

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


12 34

6/21/2007 12:17:00 AM

0

Tim Pease 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.

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

Sebastian Hungerecker

6/21/2007 8:11:00 AM

0

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

> 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


--
Ist so, weil ist so
Bleibt so, weil war so

Mariusz Pekala

6/21/2007 9:54:00 AM

0

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.

--
Ceterum censeo Internet Explorer esse delendam.

Robert Klemme

6/21/2007 10:04:00 PM

0

On 21.06.2007 11:54, Mariusz Pekala wrote:
> 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.

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