如何在默认应用程序中打开文件。 ruby

如何使用Ruby脚本在其默认应用程序中打开文件?

让我们说,我有文件夹.txt .doc .rb,我想分别用Notepad,Word和RubyMine打开它们。 我假设所有文件都打开了默认应用程序。

这应该工作(未经测试,因为我现在不在Windows机器上):

file_to_open = "c:\path\to\file.txt" system %{cmd /c "start #{file_to_open}"} 

作为参考,这也可以在OS X上完成:

 file_to_open = "/path/to/file.txt" system %{open "#{file_to_open}"} 

最好的方法是使用OS gem

 require 'os' OS.open_file_command => "start" # or open on mac, or xdg-open on linux (all designed to open a file) 

在Windows上:

 UI.openURL(filepath) 

在MacOS上:

 system %{open '#{filepath}'}