How can I get the filename from a file path in Ruby?
For example if I have a path of "C:projectslah.dll" and I just want the "blah".
"C:projectslah.dll"
Is there a LastIndexOf method in Ruby?
LastIndexOf
Try File.basename
File.basename
Returns the last component of the filename given in file_name, which must be formed using forward slashes (``/’’) regardless of the separator used on the local file system. If suffix is given and present at the end of file_name, it is removed. File.basename("/home/gumby/work/ruby.rb") #=> "ruby.rb" File.basename("/home/gumby/work/ruby.rb", ".rb") #=> "ruby"
Returns the last component of the filename given in file_name, which must be formed using forward slashes (``/’’) regardless of the separator used on the local file system. If suffix is given and present at the end of file_name, it is removed.
File.basename("/home/gumby/work/ruby.rb") #=> "ruby.rb" File.basename("/home/gumby/work/ruby.rb", ".rb") #=> "ruby"
In your case:
File.basename("C:\projects\blah.dll", ".dll") #=> "blah"
1.4m articles
1.4m replys
5 comments
56.9k users