- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我正在做一个项目,我有一个动态确定的挂载点,并提供了一组绝对路径来处理目标卷。由于这些文件尚不存在,我使用 Pathname 类来处理文件名操作。但是,在连接具有相同根的路径时,Pathname 似乎做得有点聪明。我观察到以下行为:
p1 = Pathname.new('/foo/bar') # #<Pathname:/foo/bar>
p2 = Pathname.new('/baz/quux') # #<Pathname:/baz/quux>
p3 = p1 + p2 # #<Pathname:/baz/quux>
p4 = p1.join p2.relative_path_from(Pathname.new('/')) # #<Pathname:/foo/bar/baz/quux>
p5 = Pathname.new(p1.to_s.concat p2) # #<Pathname:/foo/bar/baz/quux>
因此,对于 p4 和 p5,我可以获得我想要的行为,但结构有点做作。有没有更简洁的方法来做到这一点?
最佳答案
这里使用字符串操作来解决 Ruby 的奇怪行为相当容易。
借用OP的例子...
p1 = Pathname.new('/foo/bar')
p2 = '/baz/quux'
p1 + p2.sub(/\A\//, '')
# => #<Pathname:/foo/bar/baz/quux>
警告:第二个p2
必须是String
才能发生sub
操作。您可以使用 #to_s
轻松转换 Pathname
对象。
Pathname.new('/some/path/').to_s
# => "/some/path"
关于ruby - 将绝对路径与 Pathname 类连接起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12464361/
我正在尝试执行 vagrant up 但一直遇到此错误: ==> default: IOError: [Errno 13] Permission denied: '/usr/local/lib/pyt
我在容器 div 中有一系列动态创建的不同高度的 div。 Varying text... Varying text... Varying text... Varying text.
通过 cygwin 运行 vagrant up 时遇到以下错误。 stderr: /bin/bash: /home/vagrant/.ansible/tmp/ansible-tmp-14872260
今天要向小伙伴们介绍的是一个能够快速地把数据制作成可视化、交互页面的 Python 框架:Streamlit,分分钟让你的数据动起来! 犹记得我在做机器学习和数据分析方面的毕设时,
我是 vagrant 的新手,正在尝试将第二个磁盘添加到我正在用 vagrant 制作的虚拟机中。 我想出了如何在第一次启动虚拟机时连接磁盘,但是当我关闭机器时 然后再次备份(使用 'vagrant
我是一名优秀的程序员,十分优秀!