2014-04-26 39 views
1

我需要給上紅寶石提示錯誤的Ruby on Windows 7的變量Errno :: ENOENT:

irb(main):007:0> f = File.new("C:\Users\ABHIJI~1.SHE\AppData\Local\Temp\myoutp> 
Errno::ENOENT: No such file or directory - ls -alF C:UsersABHIJI\~1.SHEAppDataLo 
calTemp\myoutput.txt 
     from C:/Ruby/Ruby193/lib/ruby/gems/1.9.1/gems/awesome_print-1.1.0/lib/aw 
esome_print/formatter.rb:191:in ``' 
     from C:/Ruby/Ruby193/lib/ruby/gems/1.9.1/gems/awesome_print-1.1.0/lib/aw 
esome_print/formatter.rb:191:in `awesome_file' 
     from C:/Ruby/Ruby193/lib/ruby/gems/1.9.1/gems/awesome_print-1.1.0/lib/aw 
esome_print/formatter.rb:26:in `format' 
     from C:/Ruby/Ruby193/lib/ruby/gems/1.9.1/gems/awesome_print-1.1.0/lib/aw 
esome_print/inspector.rb:137:in `unnested' 
     from C:/Ruby/Ruby193/lib/ruby/gems/1.9.1/gems/awesome_print-1.1.0/lib/aw 
esome_print/inspector.rb:104:in `awesome' 
     from C:/Ruby/Ruby193/lib/ruby/gems/1.9.1/gems/awesome_print-1.1.0/lib/aw 
esome_print/core_ext/kernel.rb:10:in `ai' 
     from C:/Ruby/Ruby193/lib/ruby/gems/1.9.1/gems/awesome_print-1.1.0/lib/aw 
esome_print/core_ext/kernel.rb:15:in `ap' 
     from C:/Ruby/Ruby193/lib/ruby/gems/1.9.1/gems/awesome_print-1.1.0/lib/aw 
esome_print/inspector.rb:31:in `output_value' 
     from C:/Ruby/Ruby193/bin/irb:12:in `<main>' 

我已經給臨時文件夾的路徑命令。 我正在使用Windows 7和Ruby路徑是C:\ Ruby \ Ruby193 \ bin

回答

1

反斜槓字符用作「轉義字符」,它用於在字符串中插入特殊字符。例如,要插入製表位,您可以使用「\ t」。如果你想擁有一個文字\,你必須用另一個\來逃避它。所以你的字符串應該是"C:\\Users\\ABHIJI~1.SHE\\AppData\\Local\\Temp\\myoutput.txt"

例子:

# right 
>> puts "C:\\Users\\ABHIJI~1.SHE\\AppData\\Local\\Temp\\myoutput.txt" 
C:\Users\ABHIJI~1.SHE\AppData\Local\Temp\myoutput.txt 
=> nil 

# wrong 
>> puts "C:\Users\ABHIJI~1.SHE\AppData\Local\Temp\myoutput.txt" 
C:UsersABHIJI~1.SHEAppDataLocalTempmyoutput.txt 
=> nil 

但是,您也可以使用正斜線的路徑,甚至在Windows上。有關更多詳細信息,請參閱此答案https://stackoverflow.com/a/14875567/1067124

+0

f = File.new(「C://Users//ABHIJI~1.SHE//AppData//Local//Temp//myoutput.txt」,「w +」)不起作用即使正斜槓也不起作用 – Abhijit

+1

@Abhijit使用正斜槓時,只能使用一個。使用反斜槓時只需加倍。 ''C:/Users/ABHIJI~1.SHE「'相當於'」C:\\ Users \\ ABHIJI〜1.SHE「'。 –

+0

即使單斜線不工作 – Abhijit

相關問題