tokhi
tokhi

Reputation: 21618

list files with different extensions from a directory

How to list specific file extensions in a directory in one go? e.g; I just want to list all .jpg and .gif files from a directory. This is how I do for only jpeg files:

@images = Dir.glob("my/pictures/*.jpg")

how to do this for both jpg(s) and gif(s)? tnx.

Upvotes: 7

Views: 1625

Answers (1)

Arup Rakshit
Arup Rakshit

Reputation: 118271

Do as below

@images = Dir.glob("my/pictures/*.{jpg,gif}")

From documentation of {p,q}

Matches either literal p or literal q. Equivalent to pattern alternation in regexp. Matching literals may be more than one character in length. More than two literals may be specified.

Upvotes: 10

Related Questions