# What's going on here with gsub?

**URL:** <https://www.ruby-forum.com/t/whats-going-on-here-with-gsub/109838>\
**Category:** Ruby\
**Created:** [July 19, 2007, 3:12pm UTC](https://www.ruby-forum.com/t/whats-going-on-here-with-gsub/109838 "2007-07-19T15:12:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [July 19, 2007, 3:12pm UTC](https://www.ruby-forum.com/t/whats-going-on-here-with-gsub/109838/1 "2007-07-19T15:12:51Z")

</div>

Hi,  
I’m baffled by this problem. If I define a single file, I can use gsub  
to create the same name, with different extensions. But, when I do it  
with a list of files, it doesn’t work. Why is the code below giving me  
nothing but .tif files, when, I’m asking for three different file types:  
tif, png, and pdf?

Thanks,  
Peter

Work with one. . . .  
L:\tiff\cdtiff\47000\>irb  
irb(main):001:0\> tiffile = “wy47089.tif”  
=\> “wy47089.tif”  
irb(main):002:0\> pngfile = tiffile.gsub(/.tif/, “.png”)  
=\> “wy47089.png”  
irb(main):003:0\> pdffile = tiffile.gsub(/.tif/, “.pdf”)  
=\> “wy47089.pdf”  
irb(main):004:0\> puts “#{tiffile} #{pngfile} #{pdffile}”  
wy47089.tif wy47089.png wy47089.pdf

Doesn’t work with many. . . .  
L:\tiff\cdtiff\47000\>irb  
irb(main):001:0\> Dir.glob(“wy\*.tif”).each do |tiffile|  
irb(main):002:1\* pngfile = tiffile.gsub(/.tif/, “.png”)  
irb(main):003:1\> pdffile = tiffile.gsub(/.tif/, “.pdf”)  
irb(main):004:1\> puts “#{tiffile} #{pngfile} #{pdffile}”  
irb(main):005:1\> end  
WY47040.TIF WY47040.TIF WY47040.TIF  
WY47046.TIF WY47046.TIF WY47046.TIF  
WY47050.TIF WY47050.TIF WY47050.TIF  
WY47057.TIF WY47057.TIF WY47057.TIF  
WY47089.TIF WY47089.TIF WY47089.TIF  
=\> [“WY47040.TIF”, “WY47046.TIF”, “WY47050.TIF”, “WY47057.TIF”,  
“WY47089.TIF”]

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [July 19, 2007, 3:20pm UTC](https://www.ruby-forum.com/t/whats-going-on-here-with-gsub/109838/2 "2007-07-19T15:20:46Z")

</div>

Peter B. wrote:

> Hi,  
> I’m baffled by this problem. If I define a single file, I can use gsub  
> to create the same name, with different extensions. But, when I do it  
> with a list of files, it doesn’t work. Why is the code below giving me  
> nothing but .tif files, when, I’m asking for three different file types:  
> tif, png, and pdf?
> 
> Thanks,  
> Peter

“TIF” and “tif” is not the same. You probably want the //i switch which  
makes regexen case insensitive. Also I suggest you anchor your regexp,  
else you might have unwanted results. E.g. gsub(/.tif\z/i, …)

Regards  
Stefan

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [July 19, 2007, 3:31pm UTC](https://www.ruby-forum.com/t/whats-going-on-here-with-gsub/109838/3 "2007-07-19T15:31:22Z")

</div>

Stefan R. wrote:

> Peter B. wrote:
> 
> > Hi,  
> > I’m baffled by this problem. If I define a single file, I can use gsub  
> > to create the same name, with different extensions. But, when I do it  
> > with a list of files, it doesn’t work. Why is the code below giving me  
> > nothing but .tif files, when, I’m asking for three different file types:  
> > tif, png, and pdf?
> > 
> > Thanks,  
> > Peter
> 
> “TIF” and “tif” is not the same. You probably want the //i switch which  
> makes regexen case insensitive. Also I suggest you anchor your regexp,  
> else you might have unwanted results. E.g. gsub(/.tif\z/i, …)
> 
> Regards  
> Stefan

Thanks, Stefan. You’re right. I’ve been a Windows guy so long that I  
overlook the case stuff sometimes. Thanks a lot!

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [July 19, 2007, 3:38pm UTC](https://www.ruby-forum.com/t/whats-going-on-here-with-gsub/109838/4 "2007-07-19T15:38:44Z")

</div>

2007/7/19, Peter B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > > Peter
> > 
> > “TIF” and “tif” is not the same. You probably want the //i switch which  
> > makes regexen case insensitive. Also I suggest you anchor your regexp,  
> > else you might have unwanted results. E.g. gsub(/.tif\z/i, …)
> > 
> > Regards  
> > Stefan
> 
> Thanks, Stefan. You’re right. I’ve been a Windows guy so long that I  
> overlook the case stuff sometimes. Thanks a lot!

Some other remarks: in your case sub is sufficient and you should  
probably anchor the expression at the end - just to cover cases where  
the sequence occurs somewhere in between.

Kind regards

robert
