# Re: non-static way to get the filename?

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

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [May 9, 2006, 4:45pm UTC](https://www.ruby-forum.com/t/re-non-static-way-to-get-the-filename/60029/1 "2006-05-09T16:45:39Z")

</div>

> how i can not  
> 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?

I’m confused. You’re saying File.basename(file) is _too verbose_? Or  
have I misunderstood?

You can’t get much more terse than File.basename without getting cryptic  
imho.

Regards,

Dan

This communication is the property of Qwest and may contain confidential  
or  
privileged information. Unauthorized use of this communication is  
strictly  
prohibited and may be unlawful. If you have received this communication  
in error, please immediately notify the sender by reply e-mail and  
destroy  
all copies of the communication and any attachments.

---

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [May 10, 2006, 8:25am UTC](https://www.ruby-forum.com/t/re-non-static-way-to-get-the-filename/60029/2 "2006-05-10T08:25:44Z")

</div>

> I’m confused. You’re saying File.basename(file) is _too verbose_? Or  
> have I misunderstood?
> 
> You can’t get much more terse than File.basename without getting cryptic  
> imho.

Well, for me

file.basename

is not cryptic. Maybe for someone it is… However, consider this  
snippet:

1. 

File.copy(File.join(INPUTDIR,File.basename(f)),  
File.join(OUTPUTDIR,File.basename(f))

For me it is too verbose, i would like to see something along the lines  
of

1. 

File.copy(INPUTDIR, f.basename,  
OUTPUTDIR, f.basename)

(semantics: File.join the 1st param with the 2nd and the 3rd with the  
4th and copy joined\_1 to joined\_2).

or even

1. 

File.copy(File.join(INPUTDIR, f.basename),  
File.join(OUTPUTDIR, f.basename))

would be better,

Which example do you find more readable? Is 2) too cryptic for you? Well  
i have to say that coming from Java (or even Python - i am coming from  
both) there are _far more_ cryptic things in Ruby than any code snippet  
in this thread.

Cheers,  
Peter

---

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [May 10, 2006, 9:36am UTC](https://www.ruby-forum.com/t/re-non-static-way-to-get-the-filename/60029/3 "2006-05-10T09:36:18Z")

</div>

Pit C. wrote:

> string could possibly represent. The standard library “pathname” offers  
> exactly the nice syntax you propose, but uses its own class for file names.

Yep, point taken (i wrote the reply assuming that “pathname” does not  
exist).

Of course it would not be a good idea to put file-specific stuff into  
the String class, but i have thought file is a File - only now i  
havenoticed it is not! Thx for pointing it out!

SG,  
Peter

---

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [May 10, 2006, 9:11am UTC](https://www.ruby-forum.com/t/re-non-static-way-to-get-the-filename/60029/4 "2006-05-10T09:11:36Z")

</div>

Peter S. schrieb:

> Well, for me
> 
> file.basename
> 
> is not cryptic. Maybe for someone it is…

Peter, this syntax is very nice, indeed. But remember that your “file”  
variable above is a string. I don’t think it would a good idea to add an  
immense number of methods to the String class for all the things a  
string could possibly represent. The standard library “pathname” offers  
exactly the nice syntax you propose, but uses its own class for file  
names.

Regards,  
Pit
