# Non-static way to get the filename?

**URL:** <https://www.ruby-forum.com/t/non-static-way-to-get-the-filename/60021>\
**Category:** Ruby\
**Created:** [May 9, 2006, 3:08pm UTC](https://www.ruby-forum.com/t/non-static-way-to-get-the-filename/60021 "2006-05-09T15:08:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![splattael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@splattael](https://www.ruby-forum.com/u/splattael)\
**Post date:** [May 9, 2006, 3:08pm UTC](https://www.ruby-forum.com/t/non-static-way-to-get-the-filename/60021/1 "2006-05-09T15:08:26Z")

</div>

Hello all,

I am new to Ruby etc. etc. (all the usual disclaimer about how i can not  
code in Ruby yet etc), so probably because of this i was not able to  
find something like

myFile.basename  
resp  
myFile.anything\_which\_returns\_a\_sensible\_filename\_like\_string

and not

File.basename(myFile)

which i find too verbose for Ruby.  
By browsing the reference part of the PickAxe 2nd ed, i did not find  
any alternative to File.basename. Can somebody help me with this?

Thx,  
Peter

---

<div class="post-metadata">

**Author:** ![splattael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@splattael](https://www.ruby-forum.com/u/splattael)\
**Post date:** [May 9, 2006, 3:56pm UTC](https://www.ruby-forum.com/t/non-static-way-to-get-the-filename/60021/2 "2006-05-09T15:56:22Z")

</div>

On 5/9/06, Peter S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> File.basename(myFile)
> 
> which i find too verbose for Ruby.  
> By browsing the reference part of the PickAxe 2nd ed, i did not find  
> any alternative to File.basename. Can somebody help me with this?

Look up Pathname, it’s probably what you want.

---

<div class="post-metadata">

**Author:** ![splattael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@splattael](https://www.ruby-forum.com/u/splattael)\
**Post date:** [May 9, 2006, 3:52pm UTC](https://www.ruby-forum.com/t/non-static-way-to-get-the-filename/60021/3 "2006-05-09T15:52:56Z")

</div>

On Tue, 9 May 2006, Peter S. wrote:

> and not
> 
> File.basename(myFile)
> 
> which i find too verbose for Ruby.  
> By browsing the reference part of the PickAxe 2nd ed, i did not find any  
> alternative to File.basename. Can somebody help me with this?
> 
> Thx,  
> Peter

```
 harp:~ > cat a.rb
 require 'pathname'

 pn = Pathname.new __FILE__
 pn = pn.expand_path

 p pn.dirname
 p pn.basename

 tmp = Pathname.new '/tmp'
 p pn.relative_path_from(tmp)

 harp:~ > ruby a.rb
 #<Pathname:/home/ahoward>
 #<Pathname:a.rb>
 #<Pathname:../home/ahoward/a.rb>

```

and a whole lot more:

```
 harp:~ > ri Pathname
 -------------------------------------------------------- Class: 

```

Pathname

```
 Pathname
 --------
      Pathname represents a pathname which locates a file in a
      filesystem. It supports only Unix style pathnames. It does not
      represent the file itself. A Pathname can be relative or 

```

absolute.  
It’s not until you try to reference the file that it even  
matters  
whether the file exists or not.

```
      Pathname is immutable. It has no method for destructive 

```

update.

```
      The value of this class is to manipulate file path information 

```

in a  
neater way than standard Ruby provides. The examples below  
demonstrate the difference. _All_ functionality from File,  
FileTest, and some from Dir and FileUtils is included, in an  
unsurprising way. It is essentially a facade for all of these,  
and  
more.

```
 Examples
 --------
      Example 1: Using Pathname
        require 'pathname'
        p = Pathname.new("/usr/bin/ruby")
        size = p.size # 27662
        isdir = p.directory? # false
        dir = p.dirname # Pathname:/usr/bin
        base = p.basename # Pathname:ruby
        dir, base = p.split # [Pathname:/usr/bin, 

```

Pathname:ruby]  
data = p.read  
p.open { |f| \_ }  
p.each\_line { |line| \_ }

```
      Example 2: Using standard Ruby
        p = "/usr/bin/ruby"
        size = File.size(p) # 27662
        isdir = File.directory?(p) # false
        dir = File.dirname(p) # "/usr/bin"
        base = File.basename(p) # "ruby"
        dir, base = File.split(p) # ["/usr/bin", "ruby"]
        data = File.read(p)
        File.open(p) { |f| _ }
        File.foreach(p) { |line| _ }

      Example 3: Special features
        p1 = Pathname.new("/usr/lib") # Pathname:/usr/lib
        p2 = p1 + "ruby/1.8" # Pathname:/usr/lib/ruby/1.8
        p3 = p1.parent # Pathname:/usr
        p4 = p2.relative_path_from(p3) # Pathname:lib/ruby/1.8
        pwd = Pathname.pwd # Pathname:/home/gavin
        pwd.absolute? # true
        p5 = Pathname.new "." # Pathname:.
        p5 = p5 + "music/../articles" # Pathname:music/../articles
        p5.cleanpath # Pathname:articles
        p5.realpath # 

```

Pathname:/home/gavin/articles  
p5.children #  
[Pathname:/home/gavin/articles/linux, …]

```
 Breakdown of functionality
 --------------------------
      Core methods
      These methods are effectively manipulating a String, because 

```

that’s  
all a path is. Except for #mountpoint?, #children, and  
#realpath,  
they don’t access the filesystem.

```
      * +

```

regards.

-a
