# File.basename

**URL:** <https://rubytalk.org/t/file-basename/59276>\
**Category:** ruby-talk\
**Created:** [20 July 2010 14:08 UTC](https://rubytalk.org/t/file-basename/59276 "2010-07-20T14:08:40Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![James\_O\_Brien](https://avatars.discourse-cdn.com/v4/letter/j/5daacb/32.png) [@James\_O\_Brien](https://rubytalk.org/u/James_O_Brien)\
**Post date:** [20 July 2010 14:08 UTC](https://rubytalk.org/t/file-basename/59276/1 "2010-07-20T14:08:40Z")

</div>

File.open('myfile') do |f|  
&nbsp;&nbsp;&nbsp;&nbsp;puts f.basename;  
end

myfile exists on the filesystem but this code blows up with

undefined method `basename'

could someone explain why (given the docs  
[http://ruby-doc.org/core/classes/File.html](http://ruby-doc.org/core/classes/File.html)  
advertise the basename method)

[ruby 1.8.7 (2010-01-10 patchlevel 249) [i486-linux]]

Thanks!

---

<div class="post-metadata">

**Author:** ![MrZombie](https://avatars.discourse-cdn.com/v4/letter/m/258eb7/32.png) [@MrZombie](https://rubytalk.org/u/MrZombie)\
**Post date:** [20 July 2010 14:30 UTC](https://rubytalk.org/t/file-basename/59276/2 "2010-07-20T14:30:04Z")

</div>

You do have FileUtils installed and required, yes?

require 'ftools'

> **···**
>
> On 2010-07-20 10:08:40 -0400, James O'Brien said:
> 
> > [Note: parts of this message were removed to make it a legal post.]
> > 
> > File.open('myfile') do |f|  
> > &nbsp;&nbsp;&nbsp;&nbsp;puts f.basename;  
> > end
> > 
> > myfile exists on the filesystem but this code blows up with
> > 
> > undefined method `basename'
> > 
> > could someone explain why (given the docs  
> > [class File - RDoc Documentation](http://ruby-doc.org/core/classes/File.html)  
> > advertise the basename method)
> > 
> > [ruby 1.8.7 (2010-01-10 patchlevel 249) [i486-linux]]
> > 
> > Thanks!
> 
> --  
> Thank you for your brain.  
> -MrZombie

---

<div class="post-metadata">

**Author:** ![Daniel\_Bye](https://avatars.discourse-cdn.com/v4/letter/d/e19adc/32.png) [@Daniel\_Bye](https://rubytalk.org/u/Daniel_Bye)\
**Post date:** [20 July 2010 14:39 UTC](https://rubytalk.org/t/file-basename/59276/3 "2010-07-20T14:39:19Z")

</div>

It's a class method, not an instance method.

File.open('myfile') do |f|  
&nbsp;&nbsp;puts File.basename(f)  
end

> **···**
>
> On Tue, Jul 20, 2010 at 11:08:40PM +0900, James O'Brien wrote:
> 
> > File.open('myfile') do |f|  
> > &nbsp;&nbsp;&nbsp;&nbsp;puts f.basename;  
> > end
> > 
> > myfile exists on the filesystem but this code blows up with
> > 
> > undefined method `basename'
> > 
> > could someone explain why (given the docs  
> > [class File - RDoc Documentation](http://ruby-doc.org/core/classes/File.html)  
> > advertise the basename method)
> 
> --  
> Daniel Bye  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ASCII ribbon campaign ( )  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- against HTML, vCards and X  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- proprietary attachments in e-mail / \

---

<div class="post-metadata">

**Author:** ![Yaser\_Sulaiman](https://avatars.discourse-cdn.com/v4/letter/y/7c8e57/32.png) [@Yaser\_Sulaiman](https://rubytalk.org/u/Yaser_Sulaiman)\
**Post date:** [20 July 2010 14:41 UTC](https://rubytalk.org/t/file-basename/59276/4 "2010-07-20T14:41:01Z")

</div>

Because it's a class method, not an instance method. That is, you should use  
it as follows:

File.basename("foo.bar")

HTH,  
Yaser

> **···**
>
> On Tue, Jul 20, 2010 at 5:08 PM, James O'Brien \<jeob32@gmail.com\> wrote:
> 
> > File.open('myfile') do |f|  
> > &nbsp;&nbsp;&nbsp;puts f.basename;  
> > end
> > 
> > myfile exists on the filesystem but this code blows up with
> > 
> > undefined method `basename'
> > 
> > could someone explain why (given the docs  
> > [class File - RDoc Documentation](http://ruby-doc.org/core/classes/File.html)  
> > advertise the basename method)

---

<div class="post-metadata">

**Author:** ![Rob\_Biedenharn1](https://avatars.discourse-cdn.com/v4/letter/r/57b2e6/32.png) [@Rob\_Biedenharn1](https://rubytalk.org/u/Rob_Biedenharn1)\
**Post date:** [20 July 2010 14:52 UTC](https://rubytalk.org/t/file-basename/59276/5 "2010-07-20T14:52:03Z")

</div>

You're looking at the docs for the class method File.basename, but you're calling basename on an instance of File referenced by f

puts File.basename('myfile')

or better:

puts File.basename('/some/long/path/to/myfile')

-Rob

Rob Biedenharn   
Rob@AgileConsultingLLC.com [http://AgileConsultingLLC.com/](http://AgileConsultingLLC.com/)  
rab@GaslightSoftware.com [http://GaslightSoftware.com/](http://GaslightSoftware.com/)

> **···**
>
> On Jul 20, 2010, at 10:08 AM, James O'Brien wrote:
> 
> > File.open('myfile') do |f|  
> > &nbsp;&nbsp;&nbsp;puts f.basename;  
> > end
> > 
> > myfile exists on the filesystem but this code blows up with
> > 
> > undefined method `basename'
> > 
> > could someone explain why (given the docs  
> > [class File - RDoc Documentation](http://ruby-doc.org/core/classes/File.html)  
> > advertise the basename method)
> > 
> > [ruby 1.8.7 (2010-01-10 patchlevel 249) [i486-linux]]
> > 
> > Thanks!

---

<div class="post-metadata">

**Author:** ![Rob\_Biedenharn1](https://avatars.discourse-cdn.com/v4/letter/r/57b2e6/32.png) [@Rob\_Biedenharn1](https://rubytalk.org/u/Rob_Biedenharn1)\
**Post date:** [20 July 2010 15:03 UTC](https://rubytalk.org/t/file-basename/59276/6 "2010-07-20T15:03:44Z")

</div>

> > File.open('myfile') do |f|  
> > &nbsp;&nbsp;puts f.basename;  
> > end
> > 
> > myfile exists on the filesystem but this code blows up with
> > 
> > undefined method `basename'
> > 
> > could someone explain why (given the docs  
> > [class File - RDoc Documentation](http://ruby-doc.org/core/classes/File.html)  
> > advertise the basename method)
> > 
> > [ruby 1.8.7 (2010-01-10 patchlevel 249) [i486-linux]]
> > 
> > Thanks!
> 
> You're looking at the docs for the class method File.basename, but you're calling basename on an instance of File referenced by f
> 
> puts File.basename('myfile')
> 
> or better:
> 
> puts File.basename('/some/long/path/to/myfile')

Actually, a better example (and likely closer to what you expected):

puts File.basename(f.path)

> **···**
>
> On Jul 20, 2010, at 10:52 AM, Rob Biedenharn wrote:
> 
> > On Jul 20, 2010, at 10:08 AM, James O'Brien wrote:
> 
> > -Rob
> > 
> > Rob Biedenharn   
> > Rob@AgileConsultingLLC.com [http://AgileConsultingLLC.com/](http://AgileConsultingLLC.com/)  
> > rab@GaslightSoftware.com [http://GaslightSoftware.com/](http://GaslightSoftware.com/)

---

<div class="post-metadata">

**Author:** ![James\_O\_Brien](https://avatars.discourse-cdn.com/v4/letter/j/5daacb/32.png) [@James\_O\_Brien](https://rubytalk.org/u/James_O_Brien)\
**Post date:** [20 July 2010 15:07 UTC](https://rubytalk.org/t/file-basename/59276/7 "2010-07-20T15:07:59Z")

</div>

Yep yep silly me... THANKS everyone!

> **···**
>
> On Tue, Jul 20, 2010 at 3:52 PM, Rob Biedenharn \<Rob@agileconsultingllc.com\>wrote:
> 
> > On Jul 20, 2010, at 10:08 AM, James O'Brien wrote:
> > 
> > File.open('myfile') do |f|
> > 
> > > &nbsp;&nbsp;puts f.basename;  
> > > end
> > > 
> > > myfile exists on the filesystem but this code blows up with
> > > 
> > > undefined method `basename'
> > > 
> > > could someone explain why (given the docs  
> > > [class File - RDoc Documentation](http://ruby-doc.org/core/classes/File.html)  
> > > advertise the basename method)
> > > 
> > > [ruby 1.8.7 (2010-01-10 patchlevel 249) [i486-linux]]
> > > 
> > > Thanks!
> > 
> > You're looking at the docs for the class method File.basename, but you're  
> > calling basename on an instance of File referenced by f
> > 
> > puts File.basename('myfile')
> > 
> > or better:
> > 
> > puts File.basename('/some/long/path/to/myfile')
> > 
> > -Rob
> > 
> > Rob Biedenharn  
> > Rob@AgileConsultingLLC.com [http://AgileConsultingLLC.com/](http://AgileConsultingLLC.com/)  
> > rab@GaslightSoftware.com [http://GaslightSoftware.com/](http://GaslightSoftware.com/)

---

<div class="post-metadata">

**Author:** ![Dave\_Howell1](https://avatars.discourse-cdn.com/v4/letter/d/91b2a8/32.png) [@Dave\_Howell1](https://rubytalk.org/u/Dave_Howell1)\
**Post date:** [22 July 2010 06:45 UTC](https://rubytalk.org/t/file-basename/59276/8 "2010-07-22T06:45:44Z")

</div>

> Yep yep silly me... THANKS everyone!

And THAT is why I personally quit using File a long time ago. I encourage you to take a close look at Pathname, which I found put the various methods where I thought they should have been in the first place.

> > require 'pathname'  
> > myFile = Pathname.new("/some/path/to/file.ext")  
> > myFile.basename

=\> "file.ext"

.delete, .dirname, .atime, .executable?, .exists?, .extname, and on and on are properties of specific files, so having the methods on the instance, instead of the class, just makes all kinds of sense to me.

Pathname only has three class methods, including new. Vastly more intuitive to me.

> **···**
>
> On Jul 20, 2010, at 8:07 , James O'Brien wrote:
