Test equality of each element in an array

I want to see if any element of an array is equal to a file extension

Something like

require 'find'
require 'FileUtils'
src="/folder/on/drive/which/contains/other/folder/and/files"
dest ='another place'
extNot2move = %{jpg,JPG,pdf,PDF,MRW}
Find.find(src) do |fn|
  if File.file?(fn)
    if extNot2Move.{any of the elements} == File.extname(fn) ## this is
the test I'm interested in
      FileUtils.Move(src, dest)
  end
end

I think I could do it with a extNot2Move.each with a block following,
but it seems like their should something more concise.

Thanks from a Ruby newbie

···

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

if extNot2Move.{any of the elements} == File.extname(fn) ## this is
the test I'm interested in
I think I could do it with a extNot2Move.each with a block
following, but it seems like their should something more concise.

Why not use a regex?

   irb(main):002:0> File.extname('foo.jpg') =~ /\.(jpg|JPG|pdf|PDF|MRW)/
   => 0 # 0 is the position where the regex matched
   irb(main):003:0> File.extname('foo.zip') =~ /\.(jpg|JPG|pdf|PDF|MRW)/
   => nil

Thanks,
Swaroop

Surprise.

if extNot2Move.any?{|something| do something}

and, if you only want to match the extension, and trust in
File.extname, you may go with the Regexp

···

On Jun 30, 11:00 pm, 12 34 <rubyfo...@web.knobby.ws> wrote:

    if extNot2Move.{any of the elements} == File.extname(fn) ## this is

I would use Array's include? method:

ext_not_to_move = ['jpg', 'pdf', 'mrw']
if ext_not_to_move.include? File.extname(fn).downcase
  # do stuff
end

And use downcase so you don't have to include variations of each
extension (jpg, JPG, jpG, etc.).

Jeremy

···

On Jun 30, 11:00 pm, 12 34 <rubyfo...@web.knobby.ws> wrote:

I want to see if any element of an array is equal to a file extension

yermej@gmail.com wrote:

···

On Jun 30, 11:00 pm, 12 34 <rubyfo...@web.knobby.ws> wrote:

I want to see if any element of an array is equal to a file extension

I would use Array's include? method:

ext_not_to_move = ['jpg', 'pdf', 'mrw']
if ext_not_to_move.include? File.extname(fn).downcase
  # do stuff
end

And use downcase so you don't have to include variations of each
extension (jpg, JPG, jpG, etc.).

Jeremy

Thanks everyone for the suggestions. Jeremy's appeals to me because it's
the most direct. I looked at include, but didn't realize exactly how it
works. Thanks for the downcase reminder.

I had tried .any, but may not have had it all there.

Great community.

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

yermej@gmail.com wrote:

I want to see if any element of an array is equal to a file extension

I would use Array's include? method:

ext_not_to_move = ['jpg', 'pdf', 'mrw']
if ext_not_to_move.include? File.extname(fn).downcase
  # do stuff
end

Jeremy

As I said, this appealed to me, but it's not working and now that I look
at it more it didn't make sense to this Ruby newbie.

if ext_not_to_move.include? File.extname(fn).downcase

It probably is OK, but I wasn't sure of the syntax (although I don't get
an error). So I changed it to

if extNot2move.include?(File.extname(fn).downcase)

But this is always false. This construct has the advantage I could
puts #{extNot2move.include?(File.extname(fn).downcase)}

I've puts the File.extname(fn).downcase seperately, and it looks good to
me. I have some that should be true.

Partial output (::: are not Ruby, just separtors in the puts):

extNot2move.class is an Array and the elements are
.jpg,.pdf,.mrw,.png,.psd,.tif,.gif,.pict

extNot2move.include?(File.extname(fn).downcase) is false ::: Filename
is: /Volumes/2001/Tioga OCSS.x.tif ::: File.extname(fn).downcase is .tif

extNot2move.include?(File.extname(fn).downcase) is false ::: Filename
is: /Volumes/2001/BirdScript.pict ::: File.extname(fn).downcase is .pict

Thanks for any more help.

···

On Jun 30, 11:00 pm, 12 34 <rubyfo...@web.knobby.ws> wrote:

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

Ruben Medellin wrote:

···

On Jun 30, 11:00 pm, 12 34 <rubyfo...@web.knobby.ws> wrote:

    if extNot2Move.{any of the elements} == File.extname(fn) ## this is

Surprise.

if extNot2Move.any?{|something| do something}

and, if you only want to match the extension, and trust in
File.extname, you may go with the Regexp

Isn't extNot2Move.any? always true? I don't see where the test is made.

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

This works for me just fine (the exact same thing you are doing):

['.tif''].include? File.extname('/Volumes/2001/Tioga OCSS.x.tif').downcase

=> true

Are you sure you don't accidentally have any extra/weird characters in
either your extNot2Move array or your File names?

···

On 7/1/07, 12 34 <rubyforum@web.knobby.ws> wrote:

yermej@gmail.com wrote:
> On Jun 30, 11:00 pm, 12 34 <rubyfo...@web.knobby.ws> wrote:
>> I want to see if any element of an array is equal to a file extension
>>
>
> I would use Array's include? method:
>
> ext_not_to_move = ['jpg', 'pdf', 'mrw']
> if ext_not_to_move.include? File.extname(fn).downcase
> # do stuff
> end
>
> Jeremy

As I said, this appealed to me, but it's not working and now that I look
at it more it didn't make sense to this Ruby newbie.

if ext_not_to_move.include? File.extname(fn).downcase

It probably is OK, but I wasn't sure of the syntax (although I don't get
an error). So I changed it to

if extNot2move.include?(File.extname(fn).downcase)

But this is always false. This construct has the advantage I could
puts #{extNot2move.include?(File.extname(fn).downcase)}

I've puts the File.extname(fn).downcase seperately, and it looks good to
me. I have some that should be true.

Partial output (::: are not Ruby, just separtors in the puts):

extNot2move.class is an Array and the elements are
.jpg,.pdf,.mrw,.png,.psd,.tif,.gif,.pict

extNot2move.include?(File.extname(fn).downcase) is false ::: Filename
is: /Volumes/2001/Tioga OCSS.x.tif ::: File.extname(fn).downcase is .tif

extNot2move.include?(File.extname(fn).downcase) is false ::: Filename
is: /Volumes/2001/BirdScript.pict ::: File.extname(fn).downcase is .pict

Thanks for any more help.

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

Todd Benson wrote:

···

On 7/1/07, 12 34 <r> wrote:

This works for me just fine (the exact same thing you are doing):

['.tif''].include? File.extname('/Volumes/2001/Tioga
OCSS.x.tif').downcase

=> true

Are you sure you don't accidentally have any extra/weird characters in
either your extNot2Move array or your File names?

Thanks. Yes this works, and it works when I change one of the tif to txt
for example. So the problem must be in the extNot2move.any? part.

More fiddling and learning.

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

12 34 wrote:

Todd Benson wrote:

This works for me just fine (the exact same thing you are doing):

['.tif''].include? File.extname('/Volumes/2001/Tioga
OCSS.x.tif').downcase

=> true

Are you sure you don't accidentally have any extra/weird characters in
either your extNot2Move array or your File names?

Thanks. Yes this works, and it works when I change one of the tif to txt
for example. So the problem must be in the extNot2move.any? part.

More fiddling and learning.

if extNot2move.include?(File.extname(fn).downcase) # Does not work

if !['.jpg', '.pdf',
'.mrw','.png','.psd','.tif','.gif','.pict'].include?(File.extname(fn).downcase)
# seems to work

I need to check the logic of what I'm doing after the test. I've got
myself lost in trying out so many variations.

···

On 7/1/07, 12 34 <r> wrote:

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

From what you have posted, your ext_not_to_move array does not have
the '.'s before the extension name, which is required.

···

On 7/1/07, 12 34 <rubyforum@web.knobby.ws> wrote:

12 34 wrote:
> Todd Benson wrote:
>> On 7/1/07, 12 34 <r> wrote:
>>
>> This works for me just fine (the exact same thing you are doing):
>>
>> ['.tif''].include? File.extname('/Volumes/2001/Tioga
>> OCSS.x.tif').downcase
>>
>> => true
>>
>> Are you sure you don't accidentally have any extra/weird characters in
>> either your extNot2Move array or your File names?
>
> Thanks. Yes this works, and it works when I change one of the tif to txt
> for example. So the problem must be in the extNot2move.any? part.
>
> More fiddling and learning.

if extNot2move.include?(File.extname(fn).downcase) # Does not work

if !['.jpg', '.pdf',
'.mrw','.png','.psd','.tif','.gif','.pict'].include?(File.extname(fn).downcase)
# seems to work

I need to check the logic of what I'm doing after the test. I've got
myself lost in trying out so many variations.

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

--
Chris Carter
concentrationstudios.com
brynmawrcs.com

Hah! I do this all the time! I try to avoid negatives in my variable
names, but still end up doing it here or there and get totally
confused later.

Todd

···

On 7/1/07, 12 34 <rubyforum@web.knobby.ws> wrote:

12 34 wrote:
> Todd Benson wrote:
>> On 7/1/07, 12 34 <r> wrote:
>>
>> This works for me just fine (the exact same thing you are doing):
>>
>> ['.tif''].include? File.extname('/Volumes/2001/Tioga
>> OCSS.x.tif').downcase
>>
>> => true
>>
>> Are you sure you don't accidentally have any extra/weird characters in
>> either your extNot2Move array or your File names?
>
> Thanks. Yes this works, and it works when I change one of the tif to txt
> for example. So the problem must be in the extNot2move.any? part.
>
> More fiddling and learning.

if extNot2move.include?(File.extname(fn).downcase) # Does not work

if !['.jpg', '.pdf',
'.mrw','.png','.psd','.tif','.gif','.pict'].include?(File.extname(fn).downcase)
# seems to work

I need to check the logic of what I'm doing after the test. I've got
myself lost in trying out so many variations.

# if extNot2move.include?(File.extname(fn).downcase) # Does not work

···

From: list-bounce@example.com :
#
# if !['.jpg', '.pdf',
# '.mrw','.png','.psd','.tif','.gif','.pict'].include?(File.extn
# ame(fn).downcase)
# # seems to work
# I need to check the logic of what I'm doing after the test. I've got
# myself lost in trying out so many variations.

careful w your naming. You're using extNot2move, so that would mean, "the extensions of files you would NOT want to move". The File#extname method also returns the prefix dot "." as part of the extension, so you need to adjust your extension names.

try something like,
    ext = %w(jpg pdf mrw png pdf tif) # it's easy to add here
    extNot2move = ext.map{|e| "."+e} # this handles the needed "."

and then your test would be any of the ff:

   1) if ! extNot2move.include?(File.extname(fn).downcase)
   2) if not extNot2move.include?(File.extname(fn).downcase)
   3) unless extNot2move.include?(File.extname(fn).downcase)

   i'd prefer the 3rd style

on my case, i find myself usually asking the q the other way around, like "is this element in this set?"

you may try creating an #in? method
something like

module Enumerable
def in?(container)
   container.include?(self)
end
end

or

module Enumerable
  def in?(container)
     container.any?{|elt| elt==self}
  end
end

"jpg".in? extNot2move

=> false

".jpg".in? extNot2move

=> true

".jgg".in? extNot2move

=> false

File.extname("text.JPG").downcase.in? extNot2move

=> true

File.extname("text.JGG").downcase.in? extNot2move

=> false

kind regards -botp

Peña, Botp wrote:

From: list-bounce@example.com :

kind regards -botp

Thanks for the additional comments. I like adding the dot. Funny though,
"unless" is harder for me to grasp; not that I like like negative do not
includes.

PS: How did you get an email address like that? It irritates me that my
address shows in this discussion group. Nothing against this discussion
group (how could I? It's fantastic), it's the software.

···

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

On Behalf Of 12 34:
# Funny though, "unless" is harder for me to grasp;

try using it as a modifier, like

   move_files unless file_ext.in?list_not_allowed

here, unless would be like "but" or "except" or "if not". I emphasize the moving by stating it first, then the unless acts as a special case. I really shun from "if not/!" expressions since they tend produce runon double/triple negatives wc tend to twist my brain. forgive me, english is not my native language :frowning:

Btw, "if" can be used as a modifier too, like

   move_files if not file_ext.in?list_not_allowed

kind regards -botp