user792923
user792923

Reputation:

Name of file-object

Is it possible to get name of file, from object of File class?

For example, this method works good:

file = File::basename('/home/user/file.rb') 
p file # => file.rb

,but for object it doesn't work

file = File.new('/home/user/file.rb')
p file.basename
# => undefined method `basename' for #<File:/home/user/file.rb> (NoMethodError)

Upvotes: 3

Views: 5820

Answers (2)

Dogbert
Dogbert

Reputation: 222118

There's no direct method IIRC. You can do

file = File.new('/home/user/file.rb')
p File.basename(file.path)

Upvotes: 11

Mat
Mat

Reputation: 206689

You can use the path instance method. (And call File::basename on that if you want only that part.)

Upvotes: 0

Related Questions