ruby - 在 Windows 上从 `My Documents` 打开文件

标签 ruby sketchup

我正在为 sketch up 编写插件。我找不到从 Windows 上的 My Documents 目录追加文本文件的简单方法。我遇到的问题是:

  • 使用当前用户名打开
  • Windows 版本之间的差异。

我需要代码在 32 位计算机上的 Windows 7 上的 我的文档 中以追加模式打开文件。

最佳答案

您可以在 Ruby 中使用反引号 ` 对系统运行命令行。然后它将返回调用的结果。

这是官方描述 Ruby-Doc.org

Returns the standard output of running cmd in a subshell. The built-in syntax %x{...} uses this method. Sets $? to the process status.

`echo` #=> 'ECHO is on.' if ran on Windows

在 Windows 中,有 special variables您可以用来收集用户名等信息:

echo %username%

因此,您可以使用这两个来收集用户的“我的文档”文件夹的位置(前提是它们在 Windows 上)。

# Tested on Windows XP and 7
my_documents = File.join(`echo %userprofile%`.chomp, "My Documents")

注意: Ruby 中还有一个进行系统调用的方法,它是 system .但是,这会返回一个 bool 值,它不适用于您的情况。

另请注意,此解决方案适用于 Windows。

关于ruby - 在 Windows 上从 `My Documents` 打开文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14608816/

相关文章:

javascript - 有没有办法在破折号中禁用拖放?

ruby - 清理传入字符串的标准方法?

ruby-on-rails - 更改迁移文件中的列名?

c++ - 谷歌 SketchUp API

c++ - 渲染 SketchUp 模型 API

ruby - 如何使用字符串而不是符号来表示 `Symbol#to_proc` 速记

ruby-on-rails - Rails 部署未在生产中显示图像

ruby - 如何对二维数组求和

STL - 将 Sketchup (*.skp) 文件转换为 STL 或 OBJ

three.js - 将交互式模型从 Sketchup 导出到 three.js