File.basename

J

James O'Brien

[Note: parts of this message were removed to make it a legal post.]

File.open('myfile') do |f|
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
advertise the basename method)

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


Thanks!
 
M

MrZombie

You do have FileUtils installed and required, yes?

require 'ftools'

[Note: parts of this message were removed to make it a legal post.]

File.open('myfile') do |f|
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
advertise the basename method)

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


Thanks!
 
Y

Yaser Sulaiman

[Note: parts of this message were removed to make it a legal post.]

File.open('myfile') do |f|
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
advertise the basename method)

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

File.basename("foo.bar")

HTH,
Yaser
 
R

Rob Biedenharn

File.open('myfile') do |f|
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
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
(e-mail address removed) http://AgileConsultingLLC.com/
(e-mail address removed) http://GaslightSoftware.com/
 
R

Rob Biedenharn

File.open('myfile') do |f|
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
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)
 
J

James O'Brien

[Note: parts of this message were removed to make it a legal post.]

Yep yep silly me... THANKS everyone!



On Jul 20, 2010, at 10:08 AM, James O'Brien wrote:

File.open('myfile') do |f|
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
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
(e-mail address removed) http://AgileConsultingLLC.com/
(e-mail address removed) http://GaslightSoftware.com/
 
D

Dave Howell

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.=20
=3D> "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.=20

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,001
Messages
2,570,254
Members
46,850
Latest member
VMRKlaus8

Latest Threads

Top