我试图引用一个文件来使用RubyMine写入它并且我很难搞清楚它。使用完整路径时,代码在使用RubyMine运行时出错。 当我使用相同的代码并在终端中运行它时,代码可以正常使用命令:
ruby studio_game players.csv
如何在不必在RubyMine中指定完整路径的情况下识别文件?
Erring Code:
require_relative 'player'
require_relative 'game'
player1 = Player.new("moe")
player2 = Player.new("larry", 60)
player3 = Player.new("curly", 125)
knuckleheads = Game.new("Knuckleheads")
knuckleheads.load_players(ARGV.shift || 'players.csv')
错误讯息:
/Users/MNickey/.rvm/rubies/ruby-1.9.3-p448/bin/ruby -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) "/Users/MNickey/RubymineProjects/PragmaticStudio/Stooges Game/studio_game"
/Users/MNickey/RubymineProjects/PragmaticStudio/Stooges Game/game.rb:83:in `readlines': No such file or directory - players.csv (Errno::ENOENT)
from /Users/MNickey/RubymineProjects/PragmaticStudio/Stooges Game/game.rb:83:in `load_players'
from /Users/MNickey/RubymineProjects/PragmaticStudio/Stooges Game/studio_game:9:in `<top (required)>'
from -e:1:in `load'
from -e:1:in `<main>'
答案 0 :(得分:0)
knuckleheads.load_players(ARGV.shift || 'players.csv')
几乎肯定是导致问题的原因。除非您使用命令行参数为脚本配置了启动程序,否则ARGV将为空,您将获得普通的“players.csv”作为结果。
当你在命令行中运行它时,我很确定你正在运行带有参数的脚本。
要在RubyMine中设置命令行参数,请转到“运行/编辑配置”,找到脚本的启动配置,并将csv文件的路径添加到“脚本参数”输入框中。