如何从Ruby中的文件路径获取没有扩展名的文件名 [英] How to get filename without extension from file path in Ruby

查看:49
本文介绍了如何从Ruby中的文件路径获取没有扩展名的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从 Ruby 中的文件路径获取文件名?

How can I get the filename from a file path in Ruby?

例如,如果我的路径为 "C:\projects\blah.dll" 而我只想要blah".

For example if I have a path of "C:\projects\blah.dll" and I just want the "blah".

Ruby 中是否有 LastIndexOf 方法?

Is there a LastIndexOf method in Ruby?

推荐答案

require 'pathname'

Pathname.new('/opt/local/bin/ruby').basename
# => #<Pathname:ruby>

我已经很长时间没有成为 Windows 用户了,但是路径名 rdoc 说它在 Windows 上没有目录名称分隔符的问题.

I haven't been a Windows user in a long time, but the Pathname rdoc says it has no issues with directory-name separators on Windows.

这篇关于如何从Ruby中的文件路径获取没有扩展名的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆