我想将路径的单个组件作为数组获取。
我来到了这两个解决方案:
path = '/usr/share/doc/less/'
parts = path.split(File::Separator)
p parts # => ["", "usr", "share", "doc", "less"]
require 'pathname'
parts = []
Pathname.new(path).each_filename {|f| parts << f}
p parts # => ["usr", "share", "doc", "less"]
String.split()
这是健壮和便携的吗?
Pathname.each_filename()
Ruby 看起来有点冗长。但是,这应该是便携式的,对吧?
有没有更好的方法?我错过了标准 Ruby 的东西吗?