Object file name

Is it possible to get the file name from a Fileclass object ?

For example, this method works well:

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

but for the object it does not work

file = File.new('/home/user/file.rb')
p file.basename
# => undefined method `basename' for #<File:/home/user/file.rb> (NoMethodError)
+3
source share
2 answers

There is no direct IIRC method. You can do

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

You can use the instance method path. (And name File::basenameif you want only this part.)

0
source

All Articles