1

I have need to give command on ruby prompt

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>'

I have given path of temp folder. I am using windows 7 and Ruby path is C:\Ruby\Ruby193\bin

Abhijit
  • 105
  • 2
  • 9

1 Answers1

1

The backslash character is used as an "escape character", it is used to insert a special character in the string. To insert a tab stop for example, you would use "\t". If you wnat to have a literal \, you have to escape it with another \. So your string should be "C:\\Users\\ABHIJI~1.SHE\\AppData\\Local\\Temp\\myoutput.txt".

Example:

# 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

However, you can also use forward slashes in paths, even on Windows. For more details, see this answer https://stackoverflow.com/a/14875567/1067124.

Community
  • 1
  • 1
Benedikt Köppel
  • 4,853
  • 4
  • 32
  • 42
  • f = File.new("C://Users//ABHIJI~1.SHE//AppData//Local//Temp//myoutput.txt","w+") Not working Even forward slashes also not working – Abhijit Apr 26 '14 at 12:29
  • 1
    @Abhijit When using forward slashes, use only one. You only have to double them when using backslashes. `"C:/Users/ABHIJI~1.SHE"` is equivalent to `"C:\\Users\\ABHIJI~1.SHE"`. – Holger Just Apr 26 '14 at 12:30
  • can you run `dir C:\Users\ABHIJI~1.SHE\AppData\Local\Temp\` in a command line prompt to see if the folder exists? – Benedikt Köppel Apr 26 '14 at 14:15