# File.basename bug?

**URL:** <https://www.ruby-forum.com/t/file-basename-bug/167694>\
**Category:** Ruby\
**Created:** [May 11, 2009, 5:45pm UTC](https://www.ruby-forum.com/t/file-basename-bug/167694 "2009-05-11T17:45:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![lycanthrope](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@lycanthrope](https://www.ruby-forum.com/u/lycanthrope)\
**Post date:** [May 11, 2009, 5:45pm UTC](https://www.ruby-forum.com/t/file-basename-bug/167694/1 "2009-05-11T17:45:36Z")

</div>

How often do people get bitten by this?  
When operating on a file in the current directory, ‘./’ must be  
prepended to a filename for File.basename to work…

or is this unexpected behavior?

$ cat bug.rb  
DATAFORMAT\_VERSION=1

def processInputFile(inputFpath)  
puts “inputFpath #{inputFpath}”  
dirpath = File.dirname(inputFpath)

```
outputfile = File.basename(inputFpath)
outputfile.gsub!("pp_",'')
outputfile = dirpath + '/' + 'SDF_' + "#{DATAFORMAT_VERSION}" + '_' 

```

- outputfile  
puts “outputfile #{outputfile}”  
puts “inputFpath #{inputFpath}”

end

processInputFile(“pp\_a\_test\_file\_name”)  
rthompso@raker\>/home/rthompso  
$ ruby bug.rb  
inputFpath pp\_a\_test\_file\_name  
outputfile ./SDF\_1\_a\_test\_file\_name  
inputFpath a\_test\_file\_name  
rthompso@raker\>/home/rthompso  
$ ruby -v  
ruby 1.8.7 (2009-04-08 patchlevel 160) [i686-linux]

rthompso@raker\>/home/rthompso  
$ vi bug.rb  
rthompso@raker\>/home/rthompso  
$ ruby bug.rb  
inputFpath /home/rthompso/pp\_a\_test\_file\_name  
outputfile /home/rthompso/SDF\_1\_a\_test\_file\_name  
inputFpath /home/rthompso/pp\_a\_test\_file\_name

---

<div class="post-metadata">

**Author:** ![lycanthrope](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@lycanthrope](https://www.ruby-forum.com/u/lycanthrope)\
**Post date:** [May 11, 2009, 8:11pm UTC](https://www.ruby-forum.com/t/file-basename-bug/167694/2 "2009-05-11T20:11:37Z")

</div>

On Mon, May 11, 2009 at 9:14 PM, Reid T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> How often do people get bitten by this?  
> When operating on a file in the current directory, ‘./’ must be  
> prepended to a filename for File.basename to work…

Hi, i’m either not understanding your issue or not seeing it on my box:

$: cd /tmp; touch by.touch

$: ruby19 -ve “puts File.basename ‘by.touch’”  
ruby 1.9.1p0 (2009-01-30) [i386-darwin8.11.1]  
by.touch

$: ruby -ve “puts File.basename ‘by.touch’”  
ruby 1.8.7 (2008-08-11 patchlevel 72) [i686-darwin8]  
by.touch

> puts “outputfile #{outputfile}”  
> rthompso@raker\>/home/rthompso  
> $ ruby -v  
> ruby 1.8.7 (2009-04-08 patchlevel 160) [i686-linux]
> 
> rthompso@raker\>/home/rthompso  
> $ vi bug.rb  
> rthompso@raker\>/home/rthompso  
> $ ruby bug.rb  
> inputFpath /home/rthompso/pp\_a\_test\_file\_name  
> outputfile /home/rthompso/SDF\_1\_a\_test\_file\_name  
> inputFpath /home/rthompso/pp\_a\_test\_file\_name

Scanned over the above but couldn’t spot what you might be on about 🙂  
Perhaps you could clarify or wittle the example down a bit further?

cheers,  
lasitha

---

<div class="post-metadata">

**Author:** ![lycanthrope](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@lycanthrope](https://www.ruby-forum.com/u/lycanthrope)\
**Post date:** [May 11, 2009, 8:22pm UTC](https://www.ruby-forum.com/t/file-basename-bug/167694/3 "2009-05-11T20:22:48Z")

</div>

On Tue, 2009-05-12 at 03:10 +0900, lasitha wrote:

> ruby 1.9.1p0 (2009-01-30) [i386-darwin8.11.1]
> 
> > puts “inputFpath #{inputFpath}”  
> > processInputFile(“pp\_a\_test\_file\_name”)  
> > $ vi bug.rb  
> > lasitha

example 1 -\> processInputFile(“pp\_a\_test\_file\_name”)  
“pp\_a\_test\_file\_name” is a string object  
File.basename sets outputfile to the same object as  
“pp\_a\_test\_file\_name”.  
as a result the gsub! action on outputfile acts on the  
“pp\_a\_test\_file\_name” object.

example 2 -\> processInputFile("./pp\_a\_test\_file\_name")  
“./pp\_a\_test\_file\_name” is a string object  
File.basename sets outputfile to a new object as “pp\_a\_test\_file\_name”.  
as a result the gsub! action on outputfile acts on the new object,  
without modifying the original “./pp\_a\_test\_file\_name”

---

<div class="post-metadata">

**Author:** ![lycanthrope](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@lycanthrope](https://www.ruby-forum.com/u/lycanthrope)\
**Post date:** [May 11, 2009, 8:45pm UTC](https://www.ruby-forum.com/t/file-basename-bug/167694/4 "2009-05-11T20:45:48Z")

</div>

On 11.05.2009 20:21, Reid T. wrote:

> > ruby 1.9.1p0 (2009-01-30) [i386-darwin8.11.1]
> > 
> > > puts “inputFpath #{inputFpath}”  
> > > processInputFile(“pp\_a\_test\_file\_name”)  
> > > $ vi bug.rb
> 
> File.basename sets outputfile to a new object as “pp\_a\_test\_file\_name”.  
> as a result the gsub! action on outputfile acts on the new object,  
> without modifying the original “./pp\_a\_test\_file\_name”

Heureka! It’s an aliasing problem which is not there in 1.9:

$ irb19  
irb(main):001:0\> s = ‘foo.txt’  
=\> “foo.txt”  
irb(main):002:0\> x = File.basename s  
=\> “foo.txt”  
irb(main):003:0\> s.equal? x  
=\> false  
irb(main):004:0\> s.object\_id  
=\> 134667930  
irb(main):005:0\> x.object\_id  
=\> 134637100  
irb(main):006:0\>

$ irb  
irb(main):001:0\> s = ‘foo’  
=\> “foo”  
irb(main):002:0\> x = File.basename s  
=\> “foo”  
irb(main):003:0\> s.equal? x  
=\> true  
irb(main):004:0\> s.object\_id  
=\> 1073538940  
irb(main):005:0\> x.object\_id  
=\> 1073538940  
irb(main):006:0\>

Whether you consider this actually a bug of File.basename - I don’t  
know. It is at least a documentation bug because the docs do not  
mention that fact.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![lycanthrope](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@lycanthrope](https://www.ruby-forum.com/u/lycanthrope)\
**Post date:** [May 11, 2009, 8:31pm UTC](https://www.ruby-forum.com/t/file-basename-bug/167694/5 "2009-05-11T20:31:37Z")

</div>

On 11.05.2009 20:10, lasitha wrote:

> ruby 1.9.1p0 (2009-01-30) [i386-darwin8.11.1]
> 
> > puts “inputFpath #{inputFpath}”  
> > dirpath = File.dirname(inputFpath)
> > 
> > outputfile = File.basename(inputFpath)

This can be combined as

dir, out = File.split input\_path

> > inputFpath pp\_a\_test\_file\_name  
> > outputfile ./SDF\_1\_a\_test\_file\_name  
> > inputFpath a\_test\_file\_name

This output cannot come from the code above as “inputFpath” does not  
change in the method.

> > inputFpath /home/rthompso/pp\_a\_test\_file\_name
> 
> Scanned over the above but couldn’t spot what you might be on about 🙂  
> Perhaps you could clarify or wittle the example down a bit further?

I am assuming that not all tests were done with the same version of the  
code and this might have led to confusion. 🙂

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![lycanthrope](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@lycanthrope](https://www.ruby-forum.com/u/lycanthrope)\
**Post date:** [May 11, 2009, 9:34pm UTC](https://www.ruby-forum.com/t/file-basename-bug/167694/6 "2009-05-11T21:34:01Z")

</div>

On Tue, 2009-05-12 at 03:30 +0900, Robert K. wrote:

> > $: ruby19 -ve “puts File.basename ‘by.touch’”
> > 
> > > def processInputFile(inputFpath)  
> > > outputfile = dirpath + ‘/’ + ‘SDF\_’ + “#{DATAFORMAT\_VERSION}” + ‘\_’ + outputfile  
> > > inputFpath a\_test\_file\_name
> 
> This output cannot come from the code above as “inputFpath” does not  
> change in the method.  
> Not sure what you mean – this output did come from the above code.  
> inputFpath is modified by the gsub! call on outputfile

rthompso@raker\>/home/rthompso  
$ irb  
irb(main):001:0\> DATAFORMAT\_VERSION=1  
=\> 1  
irb(main):002:0\>  
irb(main):003:0\* def processInputFile(inputFpath)  
irb(main):004:1\> puts “inputFpath #{inputFpath}”  
irb(main):005:1\> dirpath = File.dirname(inputFpath)  
irb(main):006:1\>  
irb(main):007:1\* outputfile = File.basename(inputFpath)  
irb(main):008:1\> outputfile.gsub!(“pp\_”,’’)  
irb(main):009:1\> outputfile = dirpath + ‘/’ + ‘SDF\_’ +  
“#{DATAFORMAT\_VERSION}” + ‘\_’ + outputfile  
irb(main):010:1\> puts “outputfile #{outputfile}”  
irb(main):011:1\> puts “inputFpath #{inputFpath}”  
irb(main):012:1\>  
irb(main):013:1\* end  
=\> nil  
irb(main):014:0\>  
irb(main):015:0\* processInputFile(“pp\_a\_test\_file\_name”)  
inputFpath pp\_a\_test\_file\_name  
outputfile ./SDF\_1\_a\_test\_file\_name  
inputFpath a\_test\_file\_name  
=\> nil

---

<div class="post-metadata">

**Author:** ![lycanthrope](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@lycanthrope](https://www.ruby-forum.com/u/lycanthrope)\
**Post date:** [May 11, 2009, 10:35pm UTC](https://www.ruby-forum.com/t/file-basename-bug/167694/7 "2009-05-11T22:35:39Z")

</div>

On 11.05.2009 21:33, Reid T. wrote:

> On Tue, 2009-05-12 at 03:30 +0900, Robert K. wrote:

> > This output cannot come from the code above as “inputFpath” does not  
> > change in the method.  
> > Not sure what you mean – this output did come from the above code.  
> > inputFpath is modified by the gsub! call on outputfile

You are right. It’s an aliasing problem not present in 1.9.1 which I am  
using. See my other posting.

Kind regards

robert
