if == any item in an array

Discussion in 'Ruby' started by 12 34, Jun 20, 2007.

  1. 12 34

    12 34 Guest

    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-forum.com/.
    12 34, Jun 20, 2007
    #1
    1. Advertising

  2. On 6/20/07, 12 34 <> 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
    Harold Hausman, Jun 20, 2007
    #2
    1. Advertising

  3. On 6/20/07, 12 34 <> 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.denhaven2.com/
    Rick DeNatale, Jun 20, 2007
    #3
  4. 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
    --
    -- Engine Yard, Serious Rails Hosting
    -- (866) 518-YARD (9273)
    Ezra Zygmuntowicz, Jun 20, 2007
    #4
  5. 12 34

    Tim Pease Guest

    On 6/20/07, 12 34 <> 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
    Tim Pease, Jun 20, 2007
    #5
  6. On Thu, Jun 21, 2007 at 04:40:34AM +0900, Tim Pease wrote:
    > On 6/20/07, 12 34 <> 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, Jun 20, 2007
    #6
  7. 12 34

    12 34 Guest

    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-forum.com/.
    12 34, Jun 21, 2007
    #7
  8. 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
    Sebastian Hungerecker, Jun 21, 2007
    #8
  9. --gKMricLos+KVdGMg
    Content-Type: text/plain; charset=us-ascii
    Content-Disposition: inline
    Content-Transfer-Encoding: quoted-printable

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

    > 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--
    Mariusz Pękala, Jun 21, 2007
    #9
  10. On 21.06.2007 11:54, Mariusz Pękala 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 whatI
    >> 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
    Robert Klemme, Jun 21, 2007
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ssoss

    Display Related Item or Add Item

    ssoss, Sep 17, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    482
    ssoss
    Sep 19, 2003
  2. Replies:
    2
    Views:
    606
  3. root

    item[LENGTH] vs. *item and malloc()

    root, Aug 21, 2003, in forum: C Programming
    Replies:
    2
    Views:
    381
    Eric Sosman
    Aug 21, 2003
  4. Steven Bethard
    Replies:
    0
    Views:
    386
    Steven Bethard
    Mar 12, 2005
  5. Johannes Zellner
    Replies:
    1
    Views:
    491
    Alex Martelli
    Jan 17, 2006
Loading...

Share This Page